I'm working on yet another exercise from Intermediate Perl. I've been
given a script that searches for files that fall between a two
timestamps. For the exercise, I am supposed to write the
gather_mtime_between subroutine that will return two references to two
subroutines. One will use File::Find that will find the files that
were modified between the two timestamps. The other function should
return a list of the found items.
use strict;
use File::Find;
use Time::Local;
sub gather_mtime_between {
return (
sub {
foreach my $filename(my @file_list){
my $timestamp = (stat $filename)[9];
if (my $start <= $timestamp <= my $stop){
push @found_items, $filename;
}
}
},
sub { return @found_items };
)
}
my $target_dow = 1; # Sunday is 0, Monday is 1, ...
my @starting_directories = (".");
my $seconds_per_day = 24 * 60 * 60;
my($sec, $min, $hour, $day, $mon, $yr, $dow) = localtime;
my $start = timelocal(0, 0, 0, $day, $mon, $yr); # midnight today
while ($dow != $target_dow) {
# Back up one day
$start -= $seconds_per_day; # hope no DST! :-)
if (--$dow < 0) {
$dow += 7;
}
}
my $stop = $start + $seconds_per_day;
my($gather, $yield) = gather_mtime_between($start, $stop);
find($gather, @starting_directories);
my @files = $yield->();
for my $file (@files) {
my $mtime = (stat $file)[9]; # mtime via slice
my $when = localtime $mtime;
print "$when: $file\n";
}
I have 3 questions:
1) The $start and $stop variables in my subroutine are the $start and
$stop variables in the script at large. However, I had to label these
with 'my' because I was getting error messages. Will this cause
trouble?
2) How do I get the array @starting_directories into my first
anonymous subroutine? I need @starting_directories to be in the same
place as @file_list.
3) When I run the script I get these error messages:
syntax error at last_mod.pl line 16, near "$timestamp <="
syntax error at last_mod.pl line 20, near "}"
I can't figure out what is causing these error messages. The various
braces, etc. seem to match up.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/