keithhanlan has asked for the wisdom of the Perl Monks concerning the following question:
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:
However, it doesn't seem to work in our environment for some reason and I would like to understand why.$alloutput=`cmd 2>&1`; # This should capture both stdout and stderr.
Here's a simple test:
Now, witness the following:% cat asdf #!/bin/sh echo "output to stdout" echo "output to stderr" >&2
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:% perl -e '$x=`./asdf 2>&1`;print "Captured $x"' Captured output to stdout output to stderr
Note that if I use the latter construct, it does work:$output = `cmd 2>&1`; # either with backticks $pid = open(PH, "cmd 2>&1 |"); # or with an open pipe while (<PH>) { } # plus a read
% 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
|
---|