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

Hi,

I have an external shell script (which I can’t modify) that returns messages to STDOUT and STDERR at a time.

I want to call this external shell script from my Perl program (which I can modify) and store the return values into two different Perl variables say $sout & $serr. The external script will ALWAYS return to *BOTH* STDOUT and STDERR.

Sample external shell script can be created something like this:

echo "out" >&1
echo "err" >&2

Sample Perl program can be something like this:

($sout, $serr) = callex(…);

I want to see $sout has the value “out” and $serr has the value “err”.

If this can’t be done then please let me know.

Thanks.

  • Comment on capture STDOUT, STDERR to TWO different perl variables

Replies are listed 'Best First'.
Re: capture STDOUT, STDERR to TWO different perl variables
by ikegami (Patriarch) on Feb 01, 2011 at 16:23 UTC
Re: capture STDOUT, STDERR to TWO different perl variables
by Your Mother (Archbishop) on Feb 01, 2011 at 16:48 UTC
Re: capture STDOUT, STDERR to TWO different perl variables
by fidesachates (Monk) on Feb 01, 2011 at 16:55 UTC
    Probably goes without saying, but I figure I'll point out the obvious answer.

    exec("shell.sh > stdout.txt 2> stderr.txt")

    And then just read read each file.


    The other obvious answer is
    my $output = `shell.sh 2>&1`
    but that won't separate out the stderr from the stdout.

    Providing these solutions in case you don't have the module that was already suggested and you can't install the module.