I agree with some of the monks here that having to modify files on-the-fly is fishy. The simplest solution here is to write the checksum at the end ( following the chronological order in which data is being generated ), if I own the process that consumes the file I'd definitely modify the parser to read checksum at the end instead of the beginning.

Sometimes that's just not possible and you have to follow an specification, in that case I'd create a module with the following API

package File::MyFileType; sub open { my ($class, $filename ) = @_; ... $self->_initialize_headers; # initialize headers would write something like # Checksum: XXXXXXXXXXXXXX in the first line # reserving space for file's header } sub write { my ($self, $data) = @_; $self->_update_checksum($data); } sub close { $self->_update_headers; # Update header rewinds the file and replaces the # XXXXXXXXXX with the accumulated checksum ... } 1;

Then your App would be abstracted and it'd use File::MyfileType class, class can also be extended to support read/write and the process consuming can share same interface


In reply to Re: Design/Style question about writing to a file from different functions by bluescreen
in thread Design/Style question about writing to a file from different functions by Dirk80

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.