in reply to Re^2: Buffered, bruised and broken
in thread Buffered, bruised and broken

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.