use strict; use warnings; use IPC::Open2 ; my $out = '' ; { local *STDOUT ; open(STDOUT, ">", \$out); process(); } print "\$out = \n", $out; sub process { my $pid = open2( *Reader, *Writer, "/bin/date" ); my @prog_out = <Reader> ; close Writer; close Reader; print "output from date:\n",join("|", @prog_out), "\n"; }
This will produce:
Thu Feb 28 08:08:13 CST 2008 $out = output from date:
which shows that date printed to the original STDOUT and that the print in the process subroutine printed to $out. If you don't localize STDOUT you get:
output from date: Thu Feb 28 08:09:54 CST 2008 $out =
which shows that date emitted its output through the pipe.
Do you really need to localize STDOUT and STDERR? How about just using select to select a different default file handle? If you need to capture STDERR from your sub-process, how about using IPC::Open3?
In reply to Re: Problem with IPC::Open2 when STDOUT/STDERR are redirected
by pc88mxer
in thread Problem with IPC::Open2 when STDOUT/STDERR are redirected
by jeanluca
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |