As soon as I posted this question, found the following in man perlop:

A string which is (possibly) interpolated and then executed as a system command with /bin/sh or its equivalent. Shell wildcards, pipes, and redirections will be honored. The collected standard output of the command is returned; standard error is unaffected.

Given the bold statement, I call "bad design" on the last phrase. This has been very frustrating. Perl's philosophy of sometimes guessing what I mean rather than taking what I say at face value has now officially really ticked me off. At the very least, this construct should produce a warning message!

...Original post follows...

Can somebody suggest why backticks aren't able to capture stderr in our environment?

I generally use open() with pipes to capture the output of commands but a colleague was using back-ticks and encountered behaviour that runs counter to sample code I've seen all over the place, namely:

$alloutput=`cmd 2>&1`; # This should capture both stdout and stderr.
However, it doesn't seem to work in our environment for some reason and I would like to understand why.

Here's a simple test:

% cat asdf #!/bin/sh echo "output to stdout" echo "output to stderr" >&2
Now, witness the following:
% perl -e '$x=`./asdf 2>&1`;print "Captured $x"' Captured output to stdout output to stderr
Notice how the stderr output escapes the capture. This is contrary to examples that I've seen, for example, here: How can I capture STDERR from an external command? which explicitly states:
$output = `cmd 2>&1`; # either with backticks $pid = open(PH, "cmd 2>&1 |"); # or with an open pipe while (<PH>) { } # plus a read
Note that if I use the latter construct, it does work:
% perl -e 'open(F,"./asdf 2>&1|");while($x=<F>){print "Captured $x";}' Captured output to stdout Captured output to stderr

In our environment, this is reproducible on both Linux & Solaris using perl 5.5.3, 5.8.5, and 5.12.2.

Any help or hints would be appreciated.

Thank you,
Keith


In reply to backticks fail to capture stderr even with explicit redirection by keithhanlan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.