in reply to STDERR and STDOUT

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:

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";
Just run this script and pass the command (and options) you want it to run for you, for example:
./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)

Replies are listed 'Best First'.
Re^2: STDERR and STDOUT
by chromatic (Archbishop) on Nov 18, 2005 at 18:45 UTC

    I've done this, passing the same (undefined) scalar for the STDERR and STDOUT of the child process. I don't know if it works everywhere (and I haven't tried it on a non-Unix machine), but it seems okay to me so far where I've tried it.

Re^2: STDERR and STDOUT
by Aristotle (Chancellor) on Nov 18, 2005 at 15:37 UTC

    Note that this doesn’t give him his STDOUT and STDERR interleaved as they would be on the console; which he seems to want.

    Makeshifts last the longest.