I would use IPC::Open3 for this. You can open two filehandles simultaneously, one for STDOUT and one for STDERR and collect their respective outputs into two separate scalars:
Just run this script and pass the command (and options) you want it to run for you, for example:use strict; use warnings; use IPC::Open3; my ($msg_out,$err_out); eval { open3(undef, *OUT, *ERR, @ARGV) }; if ($@) { $msg_out = ''; $err_out = "invalid command '$ARGV[0]'"; } else { $msg_out = do {local $/;<OUT>}; $err_out = do {local $/;<ERR>}; } print "STDOUT: $msg_out\n"; print "STDERR: $err_out\n";
./foo.pl ls -la ./foo.pl some_command_that_does_not_exist ./foo.pl perl -le 'die "goodbye cruel world"'etc. IPC::Open3 does a lot more than just this, be sure and read the docs as you might need to wait for and reap the child process yourself. YMMV. :)
UPDATE: Good point, Aristotle. This code indeed does not record STDOUT and STDERR as they occur, but is that really a reasonable request? Have you ever had need for such? I can't say that i have ... :-/
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
In reply to Re: STDERR and STDOUT
by jeffa
in thread STDERR and STDOUT
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |