explain code section please...
explain code section please...
am 28.08.2006 22:41:48 von lancerset
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 ?? 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";
Re: explain code section please...
am 28.08.2006 23:49:32 von mumia.w.18.spam+nospam.usenet
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
Re: explain code section please...
am 29.08.2006 00:35:48 von someone
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
Re: explain code section please...
am 29.08.2006 02:26:00 von lancerset
Thanks for the replies, it is very helpful. One more question, is the
subroutine called for as many elements that there are in the bin
directory, or is the subroutine being invoked only once and the
contents of bin are being copied into the subroutine to be worked on
Thanks
John W. Krahn wrote:
> 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
Re: explain code section please...
am 29.08.2006 03:00:50 von someone
onlineviewer wrote:
>
> John W. Krahn wrote:
>>
>>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.
>
> Thanks for the replies, it is very helpful. One more question, is the
> subroutine called for as many elements that there are in the bin
> directory,
Yes.
> or is the subroutine being invoked only once and the
> contents of bin are being copied into the subroutine to be worked on
No.
John
--
use Perl;
program
fulfillment
Re: explain code section please...
am 29.08.2006 03:06:22 von lancerset
thx,
John W. Krahn wrote:
> onlineviewer wrote:
> >
> > John W. Krahn wrote:
> >>
> >>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.
> >
> > Thanks for the replies, it is very helpful. One more question, is the
> > subroutine called for as many elements that there are in the bin
> > directory,
>
> Yes.
>
> > or is the subroutine being invoked only once and the
> > contents of bin are being copied into the subroutine to be worked on
>
> No.
>
>
> John
> --
> use Perl;
> program
> fulfillment
Re: explain code section please...
am 29.08.2006 03:09:09 von lancerset
thx
John W. Krahn wrote:
> onlineviewer wrote:
> >
> > John W. Krahn wrote:
> >>
> >>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.
> >
> > Thanks for the replies, it is very helpful. One more question, is the
> > subroutine called for as many elements that there are in the bin
> > directory,
>
> Yes.
>
> > or is the subroutine being invoked only once and the
> > contents of bin are being copied into the subroutine to be worked on
>
> No.
>
>
> John
> --
> use Perl;
> program
> fulfillment