But that solution, in itself, raises another question. Why should setting $| fix the problem ?

Output buffers are not provided by the OS. Each library that does output buffering provides its own system. When you are using the C lib to do output, you use its buffers. PerlIO apparently does not use the C lib for output and thus has independent buffers.

The real solution is to use the functions documented in perlapio instead of the C output functions. ( Oops, you already said that )

Setting $| should flush only the perl buffer, shouldn't it ? And that buffer was already being flushed by the trailing "\n"

"\n" only flushes STDOUT when it's connected to a terminal. By redirecting the output, you turn on full buffering. $|=1; turns it off, returning to a situation similar to when you didn't redirect STDOUT (but different since it's not even line buffered now).

STDOUT->autoflush(0)STDOUT is connected to a terminalSTDOUT is line buffered (flushed by "\n")
STDOUT->autoflush(0)STDOUT isn't connected to a terminalSTDOUT is block buffered (not flushed by "\n")
STDOUT->autoflush(1)STDOUT isn't buffered (flushed after every print)
 
STDERR->autoflush(0)STDERR isn't buffered (flushed after every print)
STDERR->autoflush(1)STDERR isn't buffered (flushed after every print)
 
$fh->autoflush(0)$fh is block buffered (not flushed by "\n")
$fh->autoflush(1)$fh isn't buffered (flushed after every print)

(Where $fh is any handle other than STDOUT and STDERR)

Update: Apparently, one cannot turn off autoflush on STDERR, so I updated the table.


In reply to Re^3: Buffered, bruised and broken by ikegami
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.