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

In reply to Re: Delayed writes by nothingmuch
in thread Delayed writes by SpaceAce

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.