(This is cross-posted on devshed and perlguru due to not getting much answers)

This is a similar question to my previous one about modules but a more overhead question. The example is somewhat simplified.

Say you have a bunch of scripts like request.pl, modify.pl, transfer.pl, pending.pl etc.....
they all contain multiple subs where it reads a directory or contents of a comma separated file and will check for a variety of conditions so it can go on to modify or transfer or view etc.

Right now every script and alot of subs in them re-read the file or files in question, so alot of scripts have something like this, using a foreach, sometimes a while...etc

foreach my $file (@files) { chomp $file; my $filename = "$file"; (open(my $IF, '<', $filename)); my @entry = <$IF>; $lastentry = $entry[-1]; close $IF; chomp $lastentry; my @record = split(':', $lastentry); my $id = $record[0]; my $name = $record[1]; my $street = $record[2]; my $city = $record[3]; my $state = $record[4]; my $status = $record[5]; if (some condition like $status is active)( do something..... )

So I thought instead of all this re-reading, what if a module was called to just read the files once and do different things in different scripts. Read the files, split up the commas in to variables or something and have it in an array or hash....and then based on conditions do something

Example: in the modify script read the files and determine which are in status active and do stuff, and in move.pl determine if city is something specific and do something else with it but not have to re-read the file in each applicable sub and script. I'm sorta missing something because in all cases the file being read is in a while or for loop so what do I do? Just return the array from the module and split in each file or what? Does this make sense?

How would you do this?

---Iconx

In reply to Discussion(maybe?) How would you do this? Modules and reading a file/dir by Anonymous Monk

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.