Re: explain code section please...



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: Map out a directory heirarchy
    ... Harry Putnam wrote: ... >>Do the same thing but the first uses a reference to a named ... The first one creates an entry in the symbol table because it has a name while ... In the original you called getcwdinside the findsubroutine which would ...
    (perl.beginners)
  • 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)
  • Re: Q: ENTRY Argument List
    ... > I have used a compiler in the past that would not allow differing ENTRY ... > statement argument types from the subroutine. ... reference the argument list directly, ...
    (comp.lang.fortran)
  • Re: explain code section please...
    ... subroutine called for as many elements that there are in the bin ... contents of bin are being copied into the subroutine to be worked on ... I see that the $callback ... $callback is a reference to an anonymous sub that is CREATED by ...
    (comp.lang.perl.modules)
  • 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)