I think broquaint's solution will prove to be the most efficient, and will tend to impose the smallest load on resources. Of course, if the file gets really big, it'll take longer to re-write the whole thing each time you add a line or few at the top -- but the growth in delay will be relatively minor and nothing else will blow up, because you're not trying to hold the whole thing in memory (which is what would happen with coec's suggestion for using Tie::File. (There might be a way to use Tie::File that wouldn't involve holding the entire file contents in memory, but broquaint's approach is just easier.)

Apart from that, you might consider looking at the problem a different way: keep the file i/o simple (always append new text at the end of the file, the way God intended), and just change how you read and manipulate the file data for display.

If the people reading the display are only interested in the most recent content, you might decide that they won't want/need to look any farther than the "N" most recent lines. If you have the "tail" utility (everybody should have this by now), why not use it, since it was created to do just what you want (mostly):

my @latest = reverse( `tail -$n $datafile` ); # @latest has the last $n lines from $datafile, last line first...
If you're offended by the use of backticks, you might consider estimating how many bytes would likely cover $n lines, seek to that many bytes from the end of the file, and then read to the end of the file in the usual way assigning lines to array elements, and reverse the array; the oldest line might be just a fragment, but you could just ignore that one. (And $n could even be user-specified.)

(update: As usual, BrowserUK has provided a more sensible and effective approach. I'd follow his advice.)


In reply to Re: writing to the top of a file by graff
in thread writing to the top of a file by Anonymous Monk

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.