Click here to get back home

explain code section please...

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
explain code section please... onlineviewer 08-28-2006
Posted by onlineviewer on August 28, 2006, 4:41 pm
Please log in for more thread options


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";


Posted by Mumia W. on August 28, 2006, 5:49 pm
Please log in for more thread options


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


Posted by John W. Krahn on August 28, 2006, 6:35 pm
Please log in for more thread options


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

Posted by onlineviewer on August 28, 2006, 8:26 pm
Please log in for more thread options


> Peter Billam wrote:
>> Greetings xs folk. Suppose I have a bug-fix in a .xs file looking like
>> v0 &= 0xffffffff;
>> which is only necessary on 64-bit architectures... For speed reasons
>> I'd like to not include that line on 32-bit machines. Is there some
>> convenient
>> #ifdef 64whatever
>> v0 &= 0xffffffff;
>> #endif
>> defined by the xs that I can use ?
>
> Why? Just add -DMY_64bit_FIX_NEEDED in Makefile.PL
>
OK, I think I get it...
You mean in Makefile.PL have, if ($Config) then
DEFINE => '-DMY_64bit_FIX_NEEDED',
and then put
#ifdef MY_64bit_FIX_NEEDED
v0 &= 0xffffffff;
#endif
in the .xs ?

> after inspecting %Config *for the conditions you need*,
> not some "random thing" being 64-bit...
>
Presumably in this case I'd be looking for $Config ?

Thanks for your help, Peter

--
AUS/TAS/DPIW/CIT/Servers hbt/lnd/l8 6233 3061 http://www.pjb.com.au
Pasaré, pasarémos dice el agua y canta la verdad contra la piedra
-- Pablo Neruda

Posted by John W. Krahn on August 28, 2006, 9:00 pm
Please log in for more thread options


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

Similar ThreadsPosted
Does "my" vs "our" explain this problem? February 8, 2006, 11:01 am
ANNOUNCE: Code::Dumper - a ::Dumper for code. Available on CPAN November 21, 2005, 11:03 am
Please help with this code June 10, 2007, 3:40 pm
what is wrong in this code November 13, 2005, 10:40 pm
Image Magick code help November 29, 2004, 12:14 pm
Image Magick code help December 3, 2004, 6:43 pm
Net::Cmd::code doesn't return value of Net::Cmd::dataend January 10, 2005, 9:17 pm
encode the perl code September 6, 2006, 1:12 am
Using a DBI connection in many places (in the code) August 4, 2008, 4:39 am
best module to parse / modify C-code October 7, 2005, 1:27 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap