I agree with the other posters who are suggesting more abstraction. If you disentangle the operations you need to do to perform the file manipulations from the actual things to be accomplished, I think you'll find that it makes the program as a whole decidedly simpler, and wrapping it up in an object seems like a foregone conclusion. Something like this:
sub func1 { my $file_manager = File::Manipulator->new($filename); $file_manager->write_line(...); # maybe other writes ... $file_manager->move_line($from_position, $to_position); func2($file_manager); func3($file_manager); $file_manager->move_line($from_position2, $to_position2); }
Now you have a clear separation of roles: File::Manager just diddles with the file in whatever way makes the most sense, while the functions tell File::Manager what they want done.

This means that the low-level solution in File::Manager can be swapped around to anything that makes the process work without impacting the logic of what the main program wants done. Plus, you have the advantage that you can test the two parts better: the File::Manager code can be functionally tested - I wrote a line, did it get there? I moved a line that doesn't exist, did the right thing happen? - and the mainline code can talk to a dummy version of File::Manager that just puts lines into an array so you can check if the algorithm in the main program is doing what it ought to do.


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