Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What I need to do is run an external program (this appear several times. Some are perl, python, and GNU applications). I need to catch all text from the output of the previous program if it errored out (stderr?), whether it executed properly, and its output.

This is what I have so far:
#!/usr/bin/perl -w use strict; open PIPE, "./foo.pl |"; my @output = <PIPE>; close PIPE; print "error: $?\n" if $?; print "output: $_" foreach @output; print "\nend.\n";

If $? is set, I know the program didn't execute properly. So that part is fine. However, this does NOT catch the stderr of "foo.pl", it is still printing to the console. If i can get that, I think this method will do everything that i need (as described above), correct?

Replies are listed 'Best First'.
Re: Executing external Programs
by eieio (Pilgrim) on Mar 22, 2005 at 14:48 UTC
    You need to redirect stderr:
    open PIPE, "./foo.pl 2>&1 |";
    2>&1 redirect stderr (2) to stdout (1)
Re: Executing external Programs
by eXile (Priest) on Mar 22, 2005 at 15:44 UTC
    you can use IPC::Open2 or IPC::Open3 for control over more than 1 of the 3 standard filedescriptors a process has. In this case open3 (which gives you filehandles for STDIN, STDOUT and STDERR) should do the trick.
Re: Executing external Programs
by sh1tn (Priest) on Mar 22, 2005 at 15:03 UTC
    # STDOUT reader open PIPE_1, "outer.pl 2>/dev/null |" # STDERR reader open PIPE_2, "outer.pl 1>/dev/null |" # outer.pl print STDERR "stderr!$/"; print STDOUT "stdout!$/"; ... print while <PIPE_1>; # stdout! print while <PIPE_2>; # stderr! ...


      I don't see the point in this solution. Aren't the two opens supposed to spawn two different subprocesses? In this way, you're getting the stdout from one of them and stderr from the other, but probably you don't want to do that double work (not counting that the two processes could collide, have different behaviours and outcomes, etc etc).

      But it's highly probable that I'm missing something about some interaction from the two opens :(

      Flavio.

      -- Don't fool yourself.