in reply to once again: program output and return code

tye helped me out with this code a while back on another thread, and i just wanted to finish this up with the working code, so here it is~
#!/usr/bin/perl # test1.pl use IPC::Open3; $n = "\n"; open( OUTPUT, ">&STDOUT" ) or die "Can't dup STDOUT to OUTPUT: $!$n"; open( OUTERR, ">&STDERR" ) or die "Can't dup STDERR to OUTERR: $!$n"; eval { $pid = open3("<&STDIN", \*OUTPUT, \*OUTERR, 'perl return.pl') ; $val = waitpid(-1,0); # <--- added this line }; $@ && die "ERROR: $@$n"; @results = <OUTPUT>; @errors = <OUTERR>; close OUTPUT; close OUTERR; print "---pid$n"; print $pid . $n; print "---\$?$n"; print $? . $n; # <--- prints exit val print "---results$n"; foreach(@results) { print $_ . $n }; print "---errors$n"; foreach(@errors) { print $_ . $n };

~Particle

Replies are listed 'Best First'.
Re: Re: once again: program output and return code
by barn (Initiate) on Apr 07, 2003 at 17:57 UTC
    This worked for me but I had to duplicate STDIN as well as the other two, pass the dup to open3 and close it afterwards if I wanted to call the subroutine with the open3 call more than once, else:
    Carp::croak('open3: close(main::STDIN) failed: Bad file number') cal +led at .../IPC/Open3.pm line 119 IPC::Open3::xclose('main::STDIN') called at .../IPC/Open3.pm line 23 +8 IPC::Open3::_open3('open3', 'main', '<&STDIN', 'GLOB(0xa8c04)', 'GLO +B(0xa8c88)', 'pmem 2914') called at .../IPC/Open3.pm line 249 IPC::Open3::open3('<&STDIN', 'GLOB(0xa8c04)', 'GLOB(0xa8c88)', 'pmem + 2914') called at ...

      you might find IPC::Run and IPC::Run3 of use. they make ipc much easier than the open* modules.

      ~Particle *accelerates*

Re: Re: once again: program output and return code
by Anonymous Monk on Mar 12, 2002 at 20:05 UTC
    I've recently had the same sort of conundrum on Win32 and Perl. I nifty trick I've seen is to use OS redirection to shove STDERR, to STDOUT, as most shells will do with "/bin/ls 2&>1" In that way you get ALL the output of the program, and you can still manage to obtain the exit code using $?