Hi monks, been a while since I was here. Good to be back. Before I say anymore, I know about backticks and qx and capturing STDOUT/STDERR so please don't post a response about that. Below is what I want to do (in simplified form). It seems to me it should work (i.e. catch stdout <and likewise if my example had it, catch stderr too>). I mean isn't system() just doing a fork/execvp? Shouldn't it inherit my file descriptors (in this case the one pointing to an in memory file)? I want to do it this way so that I can catch the return codes as well as the stderr/stdout of a failed command.
#!/usr/bin/perl @callout = qw( echo foo ); open(my $old_stdout,">&STDOUT"); close(STDOUT); my $stdout; open(STDOUT,">",\$stdout); my $system_return_value = system(@callout); close(STDOUT); open(STDOUT,">&",$old_stdout); close($old_stdout); print "captured: $stdout\n"; # debug unless ( $system_return_value == 0 ) { #... do some stuff, look at return code (shift by 8), # print stdout/stderr of # failed command to log, etc, etc. }

In reply to catching STDERR of a system(@callout) by agarsha

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.