First off, for the case of catching STDERR when running another program, check out IPC::Open3, which has been in the standard perl distribution for years.

Right, but this is not apropos to my question. By the way, whenever I think of IPC::Open3, I think instead of IPC::Run which is a much more tractable solution to the same problem set IMO</preaching>

As for your proposed solution. It almost works. I n order for STDERR to be effectively repointed to $module_stderr you have to explicitly close the filehandle and then reopen it. (this is a special requirement that only applies to STDOUT and STDERR when being repointed to an in-memory file). After adding this it does appear that STDERR is in fact repointed somewhere, but it's not clear to me where. It certainly isn't going into that in-memory file variable ($module_stderr). However, if you repoint STDERR to a real file rather than one of these funky in-memory file scalars, then the stderr does in fact end up in that file. Here is an example script which demonstrates the difference

my $module_stderr = ''; close STDERR; open(STDERR, '>', \$module_stderr); open F, "perl -e \"warn 'hi'\" |" or die "$!"; while( <F>) { print 'out -> '.$_."\n"; } close F; print "err -> ". $module_stderr; print "\n"; open(STDERR, '>', 'testfile') or die "$!"; open F, "perl -e \"warn 'hi'\" |" or die "$!"; while( <F>) { print 'out -> '.$_."\n"; } close F; close STDERR; print "err -> ". `cat testfile`;

The output I get on my setup (Windows XP, ActivePerl 5.8.7) is:

err -> err -> hi at -e line 1.
I'm not sure where the standard err out is going in the first case; it's not in the variable and it doesn't show up in the normal terminal output either. Maybe it's just going to /dev/null or somewhere else in the ether. Now if I could just figure out a way to get this to work without using a temporary file, I'd be a happy camper...

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."


In reply to Re^2: capturing output from open | by DrWhy
in thread capturing output from open | by DrWhy

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.