I want to capture STDOUT and STDERR of another process just like I see it when it gets dumped to the terminal (same order).

I wrote a couple test scripts:

outerr.pl
print STDOUT "out 1\n"; print STDOUT "out 2\n"; print STDOUT "out 3\n"; print STDERR "err 4\n"; print STDERR "err 5\n"; print STDERR "err 6\n"; print STDOUT "out 7\n"; print STDOUT "out 8\n"; print STDOUT "out 9\n"; print STDERR "err A\n"; print STDERR "err B\n"; print STDERR "err C\n";
when run, this script produces the obvious results (all numbers in order).

So I try to capture it all with open3, this way:

use IPC::Open3; use Symbol; $OUTERR = gensym(); $IN = gensym(); print STDOUT "running...\n"; $pid = open3($IN, $OUTERR, $OUTERR, 'outerr.pl') or die $!; while ( wait != $pid ) {}; print STDOUT "done...\n"; while (<$OUTERR>) { print STDOUT "got: $_"; } close OUTERR;

However, this does not preserve the order. Strangely, in windows, I get all 6 STDOUT's, then all 6 STDERR's - and on unix, I get all 6 STDERR's, then all 6 STDOUT's.

What gives?

In reply to IPC::Open3 confusion by xafwodahs

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.