As other have pointed out there are problematic details with modify in place. You could consider using Tie:File,
http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm.

This allows you to view file as an array and handles the details for you. The file isn't automatically all read into memory and so can work on large files. Could be some performance issues or not in your app.

Update: As another thought, you could reconsider your approach. A common way is to just log the data by appending to a file. Then have some other program that analyzes the log data to make reports. Of course this depends upon how many lines this log file will have, etc...

Another update: If performance is a consideration, appending to an existing file is pretty much the fastest way to write something to the disk. And this is essentially independent of the file size. Appending data to a 1K file is same cost as appending data to a 1GB file. If your program spews out say 1,000 lines per hour, that's only 24,000 lines per day. Not much data in the scheme of things, 10x that much is not that much. It sounds to me like you just need daily stats. Even on Windows there is the concept of a "chron" job and you can also start tasks at different priorities (from a .bat file, use the "start" command instead of just typing in the executable name). But I don't think you need that. I mean crunching through say a 250K line log file to make a report is easy.

My advice is: do something simple that functionally does what you want. If that doesn't "work" for performance reasons, then get more complex.


In reply to Re^3: Replace current line in while loop by Marshall
in thread Replace current line in while loop by walkingthecow

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.