When STDOUT is connected to a terminal then it is "hot" by default and perl flushes its buffer when "\n" is written, but when it is not connected to a terminal, as when you redirect STDOUT to the file, then it is not "hot" by default and perl does not, by default, flush its buffer when "\n" is written. You can make it "hot" by setting "$|" to a true value (e.g. 1).

You can use strace, on linux, to see when perl writes to the system. When I run your test program under strace I get the following:

[ian@alula ~]$ strace perl ./test.pl >test.log ... write(1, "From C: 1\n", 10) = 10 write(1, "From C: 3\n", 10) = 10 write(1, "From C: 5\n", 10) = 10 write(1, "From C: 7\n", 10) = 10 write(1, "From C: 9\n", 10) = 10 write(1, "From perl: 0\nFrom perl: 2\nFrom p"..., 65) = 65 exit_group(0) = ?

Note that all the output from perl appears in a single write to the system. In this case, perl flushes its partially filled buffer as it prepares to exit.

If I add "$| = 1;", then the strace output ends as follows.

write(1, "From C: 1\n", 10) = 10 write(1, "From perl: 2\n", 13) = 13 write(1, "From C: 3\n", 10) = 10 write(1, "From perl: 4\n", 13) = 13 write(1, "From C: 5\n", 10) = 10 write(1, "From perl: 6\n", 13) = 13 write(1, "From C: 7\n", 10) = 10 write(1, "From perl: 8\n", 13) = 13 write(1, "From C: 9\n", 10) = 10 exit_group(0) = ?

In this case, because STDOUT was made "hot" by setting $| to 1, perl flushed its buffer to the system every time "\n" was written.


In reply to Re^3: Buffered, bruised and broken by ig
in thread Buffered, bruised and broken by syphilis

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.