Dirk80:

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


In reply to Re: Design/Style question about writing to a file from different functions by roboticus
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.