in reply to Discussion(maybe?) How would you do this? Modules and reading a file/dir

"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