I'm a little confused. You said you are running on macosx, but your code starts with:
#!C:\Perl
That makes no sense, and it entails that you can only run the script with a command line like this:
perl path/file_name_of_script arg1 ...
(where the "path/" part is only needed if the script is not in your shell's current working directory). I would use this as the initial "shebang" line:
#!/usr/bin/perl
because macosx is really a unix OS, and in unix, perl is typically found in the /usr/bin/ directory; macosx definitely has perl in that location. With that as the shebang line, and doing the shell command "chmod +x file_name_of_script", the script becomes available as a shell command:
path/file_name_of_script arg1 ...
where the "path/" part is only needed if your shell PATH variable does not include the directory where the script is stored.
As for your question about iterating over a list of file names, a method that I find useful goes like this: the perl script expects as input a list of file names, loads those into an array, and then iterates over the array. At each iteration, if there's a problem with the file or its contents, issue a warning and skip on to the next file in the list; e.g.:
#!/usr/bin/perl
use strict;
use Getopt::Long;
my $Usage = "Usage: $0 [-p path] filename.list\n or: ls [path] | $0
+[-p path]\n";
my $path = '.';
die $Usage unless ( GetOptions( 'p=s' => \$path ) and -d $path );
die $Usage if (( @ARGV and !-f $ARGV[0] ) or ( @ARGV==0 and -t ));
# need file name args or pipeline input
my @file_list = <>; # read all input as a list of file names
chomp @file_list; # get rid of line-feeds
for my $name ( @file_list ) {
my $file = "$path/$name";
if ( ! -f $file ) {
warn "input value '$file' does not seem to be a data file; ski
+pped\n";
next;
}
if ( ! open( I, "<", $file )) {
warn "open failed for input file '$file'; skipped\n";
next;
}
...
}
There are already very good shell command tools for creating a list of file names ("ls", "find"), and for filtering lists ("grep"), so I'm inclined not to rewrite those things in a perl script that is supposed to process a list of file names.
The exception to that rule is when the script is really intended for a specific task that always involves a specific location and/or filter for getting its list of file names to work on, because in that case, I'd rather not have to repeat the selection process on the command line every time I run the script. |