我试图让执行的子进程中的 STDOUT/STDERR 通过 Perl 中的管道返回父进程。我所看到的最接近我想做的是: http ://forums.devshed.com/perl-programming-6/exec-and-redirecting-stdout-stderr-168501.html
以下是我正在尝试做的一个精简示例。我还尝试了上面链接的变体。我看不出我做错了什么...
#!/usr/bin/env perl
use strict ;
use warnings ;
my $cmd = "/usr/bin/who -a" ; # anything to stdout
pipe( READER, WRITER ) ;
my $child = fork() ;
if ( $child ) {
print "I am the parent: My pid = $$ junior = $child\n" ;
close( WRITER ) ;
my @output = <READER> ;
print @output ;
print "parent is DONE\n" ;
} else {
print "I am the child. My pid = $$\n" ;
close( READER ) ;
close( STDOUT );
close( STDERR );
*STDOUT = *WRITER ;
*STDERR = *WRITER ;
print WRITER "XXX ouput before exec....\n" ;
exec( $cmd ) or exit(1) ;
}