Re: explain code section please...
- From: "Mumia W." <mumia.w.18.spam+nospam.usenet@xxxxxxxxxxxxx>
- Date: Mon, 28 Aug 2006 21:49:32 GMT
On 08/28/2006 03:41 PM, onlineviewer wrote:
Hello All,
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' then the find method is called with the $callback reference and the bin directory. Then the subroutine executes on each of the contents of the bin directory. Is that right so far ??
Yes
Thanks...
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";
"$Total_size" is only available within the anonymous subroutine, and the sub, when called without parameters, adds the size of the current file to $total_size and, when called with parameters, outputs $total_size.
The code that returns the anonymous sub might also be written like so:
return sub {
if (@_) {
return $total_size;
} else {
my $current_size = -s $_;
if (-f $_) {
$total_size += $current_size
}
}
};
WARNING: UNTESTED CODE
.
- References:
- explain code section please...
- From: onlineviewer
- explain code section please...
- Prev by Date: explain code section please...
- Next by Date: Re: explain code section please...
- Previous by thread: explain code section please...
- Next by thread: Re: explain code section please...
- Index(es):
Relevant Pages
|