Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i am using the following bit of code:

my $return = qx/$cmd/;

this works wonderfully for stdout of the system command contained in $cmd. however, i also need to cature stderr. imbedding 2>&1 into $cmd isn't working. i am aware of ipc::open3 but am not having any luck convincing him-who-installs-perl-modules to take any action.

are there any alternatives or suggestions? - d

20050224 Janitored by Corion: Added code tags, formatting

Replies are listed 'Best First'.
Re: qx and stderr
by Fletch (Bishop) on Feb 23, 2005 at 18:50 UTC

    Erm, IPC::Open3 is core (i.e. ships with perl) and has been for a while now (see perldoc perlmodlib). You shouldn't need to convince anyone to install it unless you've got a broked install of perl itself.

    Update: Oh, and the alternative is to use 2>tmpfile and let the shell redirect stderr to a file. You then check if -s "tmpfile" and handle whatever error output if necessary.

Re: qx and stderr
by RazorbladeBidet (Friar) on Feb 23, 2005 at 18:46 UTC
    are you using strict?
    is the definition of command declared in single or double quotes?

    $output = `ls -? 2>&1` works fine for me

    Update: Hmm.. I thought that defining in double quotes using &1 would possible cause problems - hence the strict question - no matter. But:

    my $cmd = "ls -? 2>&1"; $output = qx/$cmd/; print "-" x 50, "\n", $output, "\n", "-" x 50, "\n"


    works fine for me
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
      no, i'm not using strict. but your inquiry about quoting may be my problem. this is how i'm putting a value into $cmd: qq/remsh $ctm_server -l ctmsrv "ctmshout -dest ECS -message '$msg'"/ i've got quotes on top of quotes on top of quotes (sigh). perhaps 2>&1 should be inbetween the last single and double (literal) quotes? - d
        Ohh... well, not sure what *NIX you're using, but the -a flag for remsh might help (as will the -n flag). Have you tried those?
        --------------
        It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: qx and stderr
by dragonchild (Archbishop) on Feb 23, 2005 at 18:52 UTC
    How are you embedding "2>&1" into $cmd? You might have to use single-quotes for that part.

    I would look at IPC::Cmd as well as IPC::Open3. The former provides the STDERR/STDOUT streams as lists where the latter requires filehandles. You can install them yourself, even by just cut'n'pasting the code - they're both PurePerl modules.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: qx and stderr
by perlfan (Parson) on Feb 23, 2005 at 18:45 UTC
    You can use "eval" in a try/catch sort of way......