"This is a similar question to my previous one ..."

We get lots of posts by Anonymous Monk: I've no idea which was your last one. A link would have helped (see What shortcuts can I use for linking to other information?); being logged in would also have helped. Hopefully, I'm not missing important information.

On to your question. If I knew a little more about your files and what processing each of those scripts was doing, I might have provided a different (better) suggestion; however, the following should at least provide some guidance.

Here's a module that's performs all the file parsing in one place. An appropriate handler is specified by the script using this module.

package PM::SharedFileTasks; use strict; use warnings; use autodie; my %handler_for = ( modify => \&handle_modify, move => \&handle_move, ); sub handle { my ($function, $files) = @_; for my $file (@$files) { open my $fh, '<', $file; chomp(my @record = split /:/ => (<$fh>)[-1]); print "Function: '$function'; File: '$file'\n"; $handler_for{$function}->(\@record); close $fh; } } sub handle_modify { my $status = shift->[5]; # Do something with $status, e.g. print "$status\n"; return; } sub handle_move { my $city = shift->[3]; # Do something with $city, e.g. print "$city\n"; return; } 1;

Here's how the different scripts might use this module:

#!/usr/bin/env perl use strict; use warnings; use PM::SharedFileTasks; { # In modify.pl my @files = qw{pm_1076160_1.txt pm_1076160_2.txt}; PM::SharedFileTasks::handle(modify => \@files); } { # In move.pl my @files = qw{pm_1076160_2.txt pm_1076160_3.txt}; PM::SharedFileTasks::handle(move => \@files); }

Given these files:

$ cat pm_1076160_1.txt Record 1 ... Record n-1 ID1:Name1:Street1:City1:State1:Status1 $ cat pm_1076160_2.txt Record 1 ... Record n-1 ID2:Name2:Street2:City2:State2:Status2 $ cat pm_1076160_3.txt Record 1 ... Record n-1 ID3:Name3:Street3:City3:State3:Status3

Here's the output:

Function: 'modify'; File: 'pm_1076160_1.txt' Status1 Function: 'modify'; File: 'pm_1076160_2.txt' Status2 Function: 'move'; File: 'pm_1076160_2.txt' City2 Function: 'move'; File: 'pm_1076160_3.txt' City3

-- Ken


In reply to Re: Discussion(maybe?) How would you do this? Modules and reading a file/dir by kcott
in thread 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.