in reply to Delayed writes

Since a fix was given, now an explenation:

Because the STDIO buffers are set to 4k in your libraries, whenever perl prints/writes/printfs, the data you were going to print is stored in memory. When the buffer in the memory reaches 4k, it is 'flushed', meaning a system call is made, to output the data to disk. This is in order to save on somewhat more expensive system calls.

the $| variable is the correct solution, as it forces a buffer flush after every such STDIO output command. It is, however, sensitive to the currently selected filehandle. What you have to do in order to apply it to the file's filehandle, instead of to STDOUT, is mentioned in the previous reply. Setting $| only applies to the currently selected handle, and when you select another handle, the value still remains.

A different way to handle this is to make the system call explicitly, replacing all outputs with syswrite, and all inputs with sysread (Mixing the STDIO (all other) I/O commands with the ones prefixed with 'sys' will tend to lead to confusion, as parts of the file are in memory. When an STDIO write is performed, writing is postponed until the buffer is full, and when an STDIO read is performed, the buffer is filled. This will cause syswrites to occasionally output before previous STDIO outs, and sysreads to skip bytes forward, when mixed with STDIO reads. Keep them seperate - the documentation has more on this). This will work around the buffers completely, and will have the same effect as flushing them each time around.

Hope this helps!

-nuffin
zz zZ Z Z #!perl