in reply to Design/Style question about writing to a file from different functions
I prefer solution 1. Since it's a simple output stream it would be fine. I think the simplicity of simple writes and passing around a file handle wins.
When you get to the further complication, however, I think you should consider wrapping the file I/O into a simple API that lets you concentrate on the business at hand. For that, you'd want a function to write the data, and one to perform the data move. Once you go to the trouble of having several operations you want to perform on your file, then it's worth the little extra work to wrap it up.
For example, you might wrap it up as simply as something like:
{ my $FName='default.log'; my $fh; sub _open { open $fh, '+>', $FName or die "Can't open $FName: $!"; } sub write { my $data = shift; _open() if !defined $fh; syswrite($fh, $data) or die "write() error: $FName: $!"; } sub move { my ($src_pos, $dest_pos, $bytes) = @_; sysseek($fh, $src_pos, SEEK_SET); my $data; sysread($fh,$data,$bytes); sysseek($fh, $dest_pos, SEEK_SET); syswrite($fh, $data); sysseek($fh, 0, SEEK_END); } # other stuff as needed (setting name,...) }
Of course, that's untested, and you should take the extra step to turn it into a class...
Well, that's my opinion. It's worth about what you paid for it... ;^)
...roboticus
|
|---|