I'm in the habit of forcing a flush.

Would the flush happen regardless? If so, I've picked up something new.

To extend on the earlier reply, there are 2 buffers at play here, and normally it's not prudent to worry about either of them, as BrowserUK suggested earlier. System calls are expensive, and when you needlessly open/close files, you're making the kernel do a lot more work; doing that in tight loops is a sure recipe for performance problems on loaded systems.

This said, there are times when you did something "important" like adjust a system configuration file where a power outage at that point could have disastrous consequences. In this case, you'd really need to flush/sync both the PerlIO layer and the system cache layer (all without reopening the file, which is far more wasteful.) Again, these cases tend to be rare, and I do not suggest you use them everywhere as it is a performance penalty to do so. However..

If you must flush both the PerlIO and OS buffers you would have to do something like this:

$fh->print("Some critical text that must be flushed right away."); $fh->flush; $fh->sync;

In the code above, the flush() call asks the PerlIO layer to send its buffers to the OS, and the sync() call asks the lower-level system to flush its buffers to the storage medium. This will take at minimum 2 system calls (such as write(2) and fsync(2),) and recall that system calls are expensive.

Doing this where important is one thing, but don't do it out of habit. If you want tail -f to be responsive, flush() is sufficient (smarter would be to set $fh->autoflush(1) instead.) If it must get written to disk as well, follow it up with a sync() and take the performance hit. Either/both will degrade your code's performance, so use them wisely.

And beware, because some terrible OSes or storage controllers lie to you and say the data has been written even when it hasn't. There's nothing you can do about that if you have such a device, so generally one doesn't worry about such things.


In reply to Re^4: Splitting a Blocked file in Round Robin into smaller files by Apero
in thread Splitting a Blocked file in Round Robin into smaller files by tradersjoe0

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.