Perl: Run a shell command and capture STDOUT and STDERR separately
Often I'll want to run a shell command and capture STDOUT
and STDERR
separately. I wrote a function to simplify this process:
my $x = shell_cmd($cmd);
my $exit = $x->{exit};
my $err = $x->{stderr};
my $out = $x->{stdout};
# Run a command and return STDOUT/STDERR/Exit
sub shell_cmd {
use IPC::Open3;
my ($cmd) = @_;
my ($STDIN, $STDOUT, $STDERR, $pid, $ret) = (1,2,3,undef,{});
# If it's an arrayref run it directly (no shell)
if (ref($cmd) eq 'ARRAY') {
$pid = IPC::Open3::open3($STDIN, $STDOUT, $STDERR, @$cmd);
} else {
$pid = IPC::Open3::open3($STDIN, $STDOUT, $STDERR, $cmd);
}
waitpid($pid, 0);
# Set FH slurp mode
local $/ = undef;
$ret->{exit} = int($? >> 8);
$ret->{stderr} = readline($STDERR);
$ret->{stdout} = readline($STDOUT);
$ret->{cmd} = $cmd;
return $ret;
}