darkmoo said:
Now one more question. I've completely forgetten how to grab a list of all
files in a directory and load it up into an array.
Good. That's a good thing to forget, because it's almost always a bad
idea (and reflects poor programming practice).
You are probably thinking of doing something ill-advised such as:
my @files = [something that gets a list of files];
foreach my $file( @files ) {
# do something with $file
}
That approach is not good. You should instead skip the array and do
something like this:
foreach my $file( [something that gets a list of files] ) {
# do something with $file
}
You should never (or, at least, very rarely) load up a data structure
(such as an array) and then iterate over the structure. It is (almost)
always better to simply iterate over whatever process loads up the
array (bypassing the need for the array). There are exceptions, such
as if you have a valid need to iterate over the structure more than
once (but things like that should be very uncommon - usually that would
also reflect an error in logic).
Others have given you references to [something that gets a list of
files]. If you are fortunate enough to be using *NIX, though, you might
also consider IO::All. For example, to iterate over all *.txt (case
insensitive) files recursed no more than two directories deep which are
smaller than 1000 bytes and whose first line contains the string
"spooler" (case sensitive):
use strict; use warnings;
use IO::All;
my $root_dir = "/tmp";
foreach my $file (
io($root_dir) -> filter(sub { $_->name =~ /\.txt$/i
&& $_->size < 1000
&& ($_->head)[0] =~ /spooler/
} )
-> all_files(2);
) {
#do something with $file
}
__END__