You can try redirecting STDERR to a file and then reading back from that file, for the duration of $somecommand. Perhaps something like:
use Fcntl qw(:seek); # save STDERR filehandle open(my $ORIGSTDERR, ">&", STDERR); # create temporary filehandle to catch STDERR open(my $CATCHERR, "+>", undef) or die("cannot open temp file :: $!"); open(STDERR, ">&", $CATCHERR); # redirection ## STDERR is captured here ## open(PIPE, '-|', $somecommand, $arg1, $arg2, $argN, $filename); ... # restore STDERR filehandle open(STDERR, ">&", $ORIGSTDERR); # read captured STDERR from temporary filehandle seek($CATCHERR, 0, SEEK_SET); print "From CATCHERR: $_" while <$CATCHERR>; close($CATCHERR);

Using a module I wrote (Pipe processes and Perl subroutines together), the STDERR capture can be further localized for the child process only:

use IPC::Exe; ## exports exe() & bg() use Fcntl qw(:seek); # create temporary filehandle to catch STDERR open(my $CATCHERR, "+>", undef) or die("cannot open temp file :: $!"); # $cmd is a CODE reference to exec $somecommand #my $cmd = exe sub { open(STDERR, ">&", $CATCHERR) }, $somecommand, @a +rgs, $filename; my $cmd = exe sub { open(STDERR, ">&", $CATCHERR) }, 'perl', '-le', 'p +rint "From STDOUT"; print STDERR "From STDERR"; exit 33'; my @pid_and_err = $cmd->(); # do it! my $pid = shift(@pid_and_err); # only one PID $? = pop(@pid_and_err); # last array item is $CHILD_ERROR print "PID was $pid\n"; print "Exit status was ", ($? >> 8), "\n"; # read captured STDERR from temporary filehandle seek($CATCHERR, 0, SEEK_SET); print "From CATCHERR: $_" while <$CATCHERR>; close($CATCHERR); __END__ Output: From STDOUT PID was 6914 Exit status was 33 From CATCHERR: From STDERR

In reply to Re: Pipe, STDERR, STDOUT, and friends by repellent
in thread Pipe, STDERR, STDOUT, and friends by devnul

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.