[ Team LiB ] |
A.5 Answer for Chapter 6A.5.1 Exercise (Section 6.8.1)sub gather_mtime_between { my($begin, $end) = @_; my @files; my $gatherer = sub { my $timestamp = (stat $_)[9]; unless (defined $timestamp) { warn "Can't stat $File::Find::name: $!, skipping\n"; return; } push @files, $File::Find::name if $timestamp >= $begin and $timestamp <= $end; }; my $fetcher = sub { @files }; ($gatherer, $fetcher); } This code is pretty straightforward. The main challenge is getting the item names correct. When using stat inside the callback, the filename is $_, but when returning the filename (or reporting it to the user), the name is $File::Find::name. If the stat fails for some reason, the timestamp will be undef. (That can happen, for example, if it finds a dangling symbolic link.) In that case, the callback simply warns the user and returns early. If you omit that check, you can get warnings of an undefined value during the comparison with $begin and $end. When you run the completed program with this subroutine, your output should show only file modification dates on the previous Monday (unless you changed the code to use a different day of the week, of course). |
[ Team LiB ] |