Re: passing args to sub wanted

[email protected] ("Jenda Krynicky")
Newsgroups perl.beginners
Message-ID <[email protected]>
From:           	oryann9 <[email protected]>
> Greetings, 
> 
> I posted this question on perlmonks and received some great help,
> specifically from mirod but his recent suggestion is still not
> working. 
> 
> 
> Problem: This code only works when I hard-code the size to
> search for in the routine. I try to pass arguments using @_, but it
> does not work. How do I pass $size_input so wanted sees and uses it?
> 
> 
> Mirod's help: 
> 
> You need to pass an additional parameter to wanted. The way to do this
> is to use a closure: File::Find::find({wanted => sub { wanted(
> $size_input); } }, $fs_input ) ;. This way wanted is called by the
> anonymous sub, and gets passed $size_input. 
> See Why I hate File::Find and how I (hope I) fixed it for more info.
> 
> 
> 
> I read the "why i hate...." two and three times and yet still cannot get it to work.
> thank you in advance! 
> http://www.perlmonks.org/?node_id=687008
> 
> 
> use strict;
> use warnings;
> use File::Find;
> 
> my ( @root_files, @large_files, %mounts, @mounts, ) ;
> use vars qw/*name *dir *prune/ ;
> *name   = *File::Find::name ;
> *dir    = *File::Find::dir ;
> *prune  = *File::Find::prune ;
> 
> 
> <snip>
> }
> else {
>     print "USING LAST ELSE\n";
>     my $size_input = ( int 25 * ( 1024**2 ) ) ;
>     $size_input =~ tr /\000//d ;
>     my $wanted =  make_wanted ( \&wanted_1, $size_input ) ;
>     File::Find::find( $wanted, $fs_input ) ;
>     print "\n";
> }
> 
> sub wanted_1 {
> 
>     for my $key ( sort keys %mounts ) {
>         if ( $fs_input eq $key ) {
>             @mounts =
>                 grep {$fs_input} @{ $mounts{$key} } ; ###-- HoA --###
>         }
>     }
> 
>     if ( scalar @mounts > 0 ) {
>         die "cant search...foobarbazzzzy $!" ;
>     }
>     else {
>         my ( $size_input ) = shift @_ ;
>         print $size_input,"\n";
>         my ($dev,$ino,$mode,$nlink,$uid,$gid) ;
>         (( $dev,$ino,$mode,$nlink,$uid,$gid ) = lstat($_) ) &&
>          ( $dev >= 0 ) &&
>          !( $File::Find::prune |= ($dev != $File::Find::topdev ) ) &&
>          ( int(((-s _) + 1023) / 1024 ) > $size_input ) &&
>          push ((@large_files), $name ) ;
>     }
> }
> 
> sub make_wanted {
> 
>     my $wanted = shift ;    # get the "real" wanted function
>     my @args   = @_    ;    # "freeze" the arguments
>     my $sub = sub { $wanted->( @args ); } ;  # generate the anon sub
>     return $sub ;           # return it
> }
> 
> 
> print "\n",scalar @large_files,"\n";
> exit;
> <snip>
> 
> $size_input is being printed correctly/accurately, but nothing in the array. 

If $size_input is printed correcly, them it has been passed to the 
wanted_1 fine and the problem is elsewhere.

The "condition" for the push() looks insanely complex, I bet you made 
a mistake there.

And to tell the truth .. with the number of global variables you have 
I don't understand why didn't you use $size_input as a global 
variable as well.

Jenda
===== [email protected] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.