this is more academic question, since I've found that an IPC module does what I needed.

basically i wanted to execute a system command and get its stderr, stdout, and return value. so i invoke the command with backticks in two different subroutines, one of which redirects STDERR to a variable, and the other doesnt.

when i dont touch stderr, the invoked command prints the command's stderr to the script stderr. but when i try to capture the script stderr, the command's stderr disappears altogether. this baffles me, because if the script itself sends to stderr (ie: the warn builtin), it gets captured just fine.

#!/usr/bin/perl -w use strict; use Data::Dumper; # execute command without capturing STDERR sub exec_nocap { # execute command via backticks, getting output (STDOUT) and retur +n value my $command = shift; my $out = `$command`; my $return = $?; # dump everything (in an anonymous hash) print Dumper { 'Return value' => $return, 'STDOUT' => $out, }; } # execute command, capturing STDERR sub exec_cap { # dupe STDERR, to be restored later open(my $stderr, ">&", \*STDERR) or do { print "Can't dupe STDERR: + $!\n"; exit; }; # close first (according to the perldocs) close(STDERR) or die "Can't close STDERR: $!\n"; # redirect STDERR to in-memory scalar my $err; open(STDERR, '>', \$err) or do { print "Can't redirect STDERR: $!\ +n"; exit; }; # just to demonstrate that STDERR capturing is working warn('this is a warning'); # execute command via backticks, getting output (STDOUT) and retur +n value my $command = shift; my $out = `$command`; my $return = $?; # restore original STDERR open(STDERR, ">&", $stderr) or do { print "Can't restore STDERR: $!\ +n"; exit; }; print Dumper({ 'Return value' => $return, 'STDOUT' => $out, 'Redirected STDERR' => $err, }); } exec_nocap('perl -e "die(\'this is a fatal error\')"'); print "\n"x5; exec_cap('perl -e "die(\'this is a fatal error\')"');
as i said, this is purely an academic question, but why is this happening?

In reply to capturing stderr of a command, invoked via backticks by Anonymous Monk

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.