Code Comments
Programming Forum and web based access to our favorite programming groups.
I want to use exec($prog, @args) to execute a system command without
worrying about shell escapes, but I also want to capture both stdout
and stderr from this system command. If I only wanted stdout, I
could do something like this
die "Can't fork: $!" unless defined(my $pid = open(my $read_child, '-|'));
if ($pid == 0) {
exec($prog, @argv) or die "Can't exec $prog: $!\n";
exit 0; # superfluous
}
my $output = do { local $/ = undef;, <$read_child> };
close $read_child;
if ($?) {
die "Child failed: $?"
}
In the code above, $output captures the stdout from the command
run through exec. But how does one capture stderr as well? Not
surprisingly adding the string '2>&1' to exec's arguments fails
because exec just passes this string along as one more argument to
$prog.
Any help would be much appreciated.
bill
Post Follow-up to this messageIn <cr0539$so4$1@reader1.panix.com> bill <please_post@nomail.edu> writes:
>I want to use exec($prog, @args) to execute a system command without
>worrying about shell escapes, but I also want to capture both stdout
>and stderr from this system command. If I only wanted stdout, I
>could do something like this
>die "Can't fork: $!" unless defined(my $pid = open(my $read_child, '-|'));
>if ($pid == 0) {
open (STDERR, '>&STDOUT') or die "Can't dup STDOUT\n";
> exec($prog, @argv) or die "Can't exec $prog: $!\n";
> exit 0; # superfluous
>}
>my $output = do { local $/ = undef;, <$read_child> };
>close $read_child;
>if ($?) {
> die "Child failed: $?"
>}
HTH,
Karl
--
Sent from a spam-bucket account; I check it once in a blue moon. If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.
Post Follow-up to this message"bill" <please_post@nomail.edu> wrote in message news:cr0539$so4$1@reader1.panix.com... > > I want to use exec($prog, @args) to execute a system command without > worrying about shell escapes, but I also want to capture both stdout > and stderr from this system command. Have you read the appropriate FAQ on this topic? perldoc -q stderr Paul Lalli
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.