Define "Better".

Solution one has an advantage when you use file locking, because the file can be locked until you are done writing it.

Solution two has no real advantages: You still need to pass a parameter specifying the file name. Use a file handle instead and you have solution one.

Solution two is not needed, and the new behaviour does not break solution one: Open the file in read/write mode and you can modify it while still holding a lock.

Also, rethink your concept: Why do you think you need to modify the file on disk? Modify the way you write the data into the file so it is written in correct order. Think about reducing the complexity of the routines writing the file. Split them into smaller parts that can be called in any order needed. If you have the memory, think about creating an array of lines (or any other useful data structure, like a tree) first, process it as needed, and finally write the array in one run into the file.

Show us the relevant parts of the code, perhaps you are making your own life harder than really needed.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

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