I am trying to figure out how perl determines the order in which to output STDOUT and STDERR text, both to a terminal and to a file. Consider the following code:



#!/tool/bin/perl -w use strict; my $i = 0; my @array; for ($i = 0; $i < 100; $i++) { printf("element $i of array = "); printf("%s\n", $array[$i]); }


If I run this code in a terminal, the output is as follows:



> ./stderr_test.pl Use of uninitialized value in printf at ./stderr_test.pl line 11. element 0 of array = Use of uninitialized value in printf at ./stderr_test.pl line 11. element 1 of array = Use of uninitialized value in printf at ./stderr_test.pl line 11. element 2 of array = Use of uninitialized value in printf at ./stderr_test.pl line 11. element 3 of array = Use of uninitialized value in printf at ./stderr_test.pl line 11. element 4 of array = ...


As you can see, the stdout text is interleaved with the stderr, which is what I would expect based on the order of the printf statements. If I redirect the text to a file however, the result is different:



> ./stderr_test.pl > & log


where log contains:



Use of uninitialized value in printf at ./stderr_test.pl line 13. Use of uninitialized value in printf at ./stderr_test.pl line 13. Use of uninitialized value in printf at ./stderr_test.pl line 13. ... element 0 of array = element 1 of array = element 2 of array = ...


As you can see, in this case perl first outputs all the stderr output, then outputs all the stdout output. Why does it do this?



Why is the behavior different when I output to a file vs. the terminal?



More importantly, how can I force the file output to appear in the correct order? Debugging is much easier when printed statements appear in programatic order.



I tried adding the following to my code above:



open(STDERR,'>&', STDOUT);


but that didn't make any difference in my output file.


In reply to ordering of stdout and stderr outputs by Special_K

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.