B
bill
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
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