in reply to Design/Style question about writing to a file from different functions
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
|
|---|