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

I want to run a program securely and capture its output, so instead of using ``, I use something like that:

defined (my $pid = open(FROM_CHILD, "-|")) or die "can't fork: $!"; if ($pid) { my @output = <FROM_CHILD>; wait; close FROM_CHILD; return @output; } else { exec "/usr/bin/sudo", @command; die "can't exec: $!"; }

But then, I'd like @output to contain the program's STDERR, as if I had done, using the shell, "program 2>&1". How would you go about it? Using IPC::Open3 isn't an option, I want the STDERR to be intermixed with STDOUT.

Replies are listed 'Best First'.
Re: Redirecting STDERR to STDOUT securely
by belg4mit (Prior) on Dec 12, 2001 at 21:28 UTC
    Once again the manual saves the day, open:
    defined (my $pid = open(FROM_CHILD, "-|")) or die "can't fork: $!"; if ($pid) { my @output = <FROM_CHILD>; wait; close FROM_CHILD; return @output; } else { open(STDERR, ">&STDOUT") || die "Can't dup stdout"; exec "/usr/bin/sudo", @command; die "can't exec: $!"; }

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

      Thanks a lot, I've been enlightened.
Re: Redirecting STDERR to STDOUT securely
by mortis (Pilgrim) on Dec 12, 2001 at 22:15 UTC
    Have you looked into IPC::Open2?

    IPC::Open3 sets up a read, a write, and an error handle. IPC::Open2 sets up a read and a write handle. The read handle returned from IPC::Open2 represents both the stdout and stderr of the child process.