Re: explain code section please...



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

.



Relevant Pages

  • Re: explain code section please...
    ... I see that the $callback ... $callback is a reference to an anonymous sub that is CREATED by ... subroutine executes on each of the contents of the bin directory. ... entry it calls the subroutine supplied by you and puts the name of that entry ...
    (comp.lang.perl.modules)
  • Re: from intermediate perl book on closure and callback
    ... Because $subsis a subroutine reference and you need to ... print "$dir has $sum bytes\n"; ... That is instead of generating a callback and a getter, ...
    (perl.beginners)
  • [newbie] Overlap of memory between two arrays
    ... One variable is defined within module and the memory is allocated ... in the subroutine within the module ... When i check the values of bin and rpdf, some part are shared by both. ... What should i do to allocate independent memory? ...
    (comp.lang.fortran)
  • Nonsense dialog box of "Sub or Function not defined"
    ... then based on the checked boxes on the ... I will be adding at least may be 15-18 more subroutine. ... .InsertBefore PLName ... Word 2007 Developer Reference> Visual Basic for Applications Language ...
    (microsoft.public.word.vba.general)
  • Re: Callbacks - Delphi and CVF
    ... | I have a FORTRAN DLL that does the computations and takes about hours ... | want to be able to access the callback functions address from every ... | subroutine in the fortran dll. ... SUBROUTINE ReportProgress ...
    (comp.lang.fortran)