in reply to backticks fail to capture stderr even with explicit redirection

This is why I use begin AND end delimiters on my debug output. Instead of print "Captured $x", try

print "Captured >>$x<<". <c>$ perl -le '$x=`./asdf 2>&1`;print ">>$x<<"' >>output to stdout output to stderr <<
Both are caught here, with all the perls I have available to me.

(Normally I use [$x] - don't know why I didn't do that here - I guess this just stands out more.)

Replies are listed 'Best First'.
Re^2: backticks fail to capture stderr even with explicit redirection
by keithhanlan (Initiate) on Jul 06, 2011 at 19:25 UTC
    Good point on the delimiters. My test program was flawed. In the environment where we are testing, backticks do *not* capture the stderr even when I explicitly redirect it to stdout. I'll keep digging. Somebody further down the respondent chain has pointed out an interesting bit about $Config. Thank you, Keith

      I highly doubt that. Very highly doubt it. Try using a data-dumping module to spit out exactly what you have. For example:

      $ perl -MJSON -le '$output=`./asdf`; print encode_json([$output])' output to stderr ["output to stdout\n"] $ perl -MJSON -le '$output=`./asdf 2>&1`; print encode_json([$output]) +' ["output to stdout\noutput to stderr\n"]
      or, if you don't have JSON:
      $ perl -MData::Dumper -le '$output=`./asdf`;print Dumper $output' output to stderr $VAR1 = 'output to stdout '; $ perl -MData::Dumper -le '$output=`./asdf 2>&1`;print Dumper $output' $VAR1 = 'output to stdout output to stderr ';
      You'll notice that if you omit the 2>&1, the stderr part shows up outside of the variable being dumped. What's more, it shows up first, which your original code shows is not what is happening for you. The only way that the stderr shows up after the stdout, given your asdf program, is if the stderr is successfully caught.

      I will now ask you to take back you "bad design" call in your updated original node. It is based on your flawed testing, which your own node shows to be flawed. If you want to understand why the order of the output would be different if you weren't catching stderr, I would suggest a new root-level node for that. Not because we can't answer it here, but because I think you should go back to your test code and play with it for some time to see if you can figure it out yourself (with the help of google, that's fine) before asking.