2013-08-20 46 views
6

Test::More kullanarak bazı testler yazıyorum ve baskıları test ettiğim işlevlerden biri STDERR. Çıkışı STDERR'a test etmek isterim, ancak bunun nasıl yapılacağından emin değilim. Yakın olduğumu biliyorum. Bu çalışır:STDERR'i Test :: More ile nasıl test edebilirim?

use strict; 
use warnings; 
use feature qw(say); 

close STDERR; 
open STDERR, ">", \my $error_string; 

say STDERR "This is my message"; 
say qq(The \$error_string is equal to "$error_string"); 

Bu yazdırır:

The $error_string is equal to "This is my message 
" 

Ancak, ben STDERR kapatmak istemiyoruz. Sadece onu kopyalamak istiyorum.

Bu denedim:

use strict; 
use warnings; 
use feature qw(say); 

open my $error_fh, ">", my $error_string; 
open STDERR, ">&", $error_fh; 

say STDERR "This is my message"; 
close $error_fh; 
say qq(The \$error_string is equal to "$error_string"); 

Ama $error_string boştur.

Neyi yanlış yapıyorum? (open STDERR, ">&" . fileno($error_fh) ile birlikte) Benim için

cevap

6

, open STDERR, ">&", $error_fh gerçek değer döndürmez. >& modunun dup sistem çağrısı için oldukça düzgün bir sözdizimsel şeker olabileceğini düşünüyorum. Bu, $error_fh gibi bir sözdizimi dosyası üzerinde çalışmaz.

Yerelleştirme hakkında STDERR?

{ 
    local *STDERR = *$error_fh; 
    say STDERR "something"; 
} 
# STDERR restored 
+0

Dang. Autodie'yi kullandığımı düşündüm, açtım. Bunu eklediğimde, '' GLOB (0x7fcb82829808) '' modunu '> &': '/ Badest tanımlayıcısı' ile ./test.pl satırında 11 'açamadım. STDERR'in yerelleştirilmesi hile yapmalıdır. –

2
# perl -MPerlIO::tee -MData::Printer -e 'my $output; STDERR->push_layer(tee=> \$output); warn "Danger, Will Robinson!"; p($output);' 
Danger, Will Robinson! at -e line 1. 
"Danger, Will Robinson! at -e line 1. 
" 
İlgili konular