Re: explain code section please...
- From: "John W. Krahn" <someone@xxxxxxxxxxx>
- Date: Mon, 28 Aug 2006 22:35:48 GMT
onlineviewer wrote:
Can someone please explain this code section to me. This is from the
O'reilly book. learning objects,references. I see the end result, but i
am not sure how and in what order it runs. I see that the $callback
variable is a reference to the subroutine,
'create_find_callback_that_sums_the_size'
Wrong. $callback is a reference to an anonymous sub that is CREATED by
'create_find_callback_that_sums_the_size'.
perldoc -q closure
then the find method is
called with the $callback reference and the bin directory.
Correct.
Then the
subroutine executes on each of the contents of the bin directory. Is
that right so far ?? Thanks...
File::Find::find traverses the directory tree starting at 'bin' and for each
entry it calls the subroutine supplied by you and puts the name of that entry
in the $_ variable.
use File::Find;
sub create_find_callback_that_sums_the_size {
my $total_size = 0;
return sub {
if (@_) {
return $total_size;
} else {
$total_size += -s if -f;
}
};
}
my $callback = create_find_callback_that_sums_the_size( );
find($callback, "bin");
my $total_size = $callback->("dummy");
print "total size of bin is $total_size\n";
John
--
use Perl;
program
fulfillment
.
- Follow-Ups:
- Re: explain code section please...
- From: onlineviewer
- Re: explain code section please...
- References:
- explain code section please...
- From: onlineviewer
- explain code section please...
- Prev by Date: Re: explain code section please...
- Next by Date: Re: problem installing MATH::MATLAB module
- Previous by thread: Re: explain code section please...
- Next by thread: Re: explain code section please...
- Index(es):
Relevant Pages
|