Mr. Shawn H. Corey wrote:
Chris wrote:
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.
Why do people who write these books have exercises of little practical
value?
When you say return two sub references you do mean two anonymous subs,
not just references to two conventional subs?
use strict;
use File::Find;
use Time::Local;
sub gather_mtime_between {
my @found_items = ();
return (
sub {
my $start = shift @_;
my $stop = shift @_;
my @starting_directories = @_;
# Oops, don't add this statement. Sorry, about that.
my @found_items = ();
foreach my $filename(my @file_list){
my $timestamp = (stat $filename)[9];
if (my $start <= $timestamp <= my $stop){
if( $start <= $timestamp && $timestamp <= $stop ){
push @found_items, $filename;
}
}
},
# Note that this sub does not use anything from File::Find.
sub { return @found_items };
)
}
Incorporating File::Find into them is still up to you. Hint:
find( sub { ... }, @starting_directories );
This exercise creates what is called a closure. But any closure can be
replaced by an object. Objects are easier to understand because closure
introduce another layer of compile/run to the program. They should be
avoided.
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
"If you think Terrans are comprehensible, you don't understand them."
Great Fang Talphon
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/