in reply to passing argument to sub wanted

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.

Replies are listed 'Best First'.
Re^2: passing argument to sub wanted
by mikejones (Scribe) on May 16, 2008 at 19:40 UTC
    I read the "Why I hate File::Find..." three times and adjusted my code and it seems to retain the var $size_input yet my array it not being populated. When I hard-code the number in, my array is populated as expected. I am almost begging for help b/c I cannot understand how to solve this anymore or what add'l tools to use other than Dumper and perl -d.

    Please help on this frustrating code. Thank you in advance!

    <snip> use strict; use warnings; 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.

Re^2: passing argument to sub wanted
by mikejones (Scribe) on May 15, 2008 at 15:50 UTC
    Mirod, did you have time to review my update agn reply? If you could help that would be much appreciated b/c it seems you have much experience in this! thank you!
Re^2: passing argument to sub wanted
by mikejones (Scribe) on May 14, 2008 at 15:49 UTC

    UPDATE

    Mirod's idea does not retain my $size_input and I am still having to hard-code the value. Any more ideas greatly appreciated!

    sub wanted; File::Find::find({wanted => sub { wanted( $size_input ); } }, $fs_inpu +t ) ;
    UPDATE AGN

    I changed the find code to include a dereference (->) and the value is being printed the line b4 the int -s, but now the array is not being populated with $name when it should contain 16 elements. Finally when I print *name with $size_input it does not print, but when I print *name with the hard-coded number in the int ((( -s line, I see *name incrementing.

    File::Find::find({wanted => sub { wanted->( $size_input ); } }, $fs_i +nput ) ; <snip> else { my ( $size_input ) = shift @_ ; ###-- Freeze the arg --# my ($dev,$ino,$mode,$nlink,$uid,$gid); (( $dev,$ino,$mode,$nlink,$uid,$gid ) = lstat($_) ) && ( $dev >= 0 ) && !( $File::Find::prune |= ($dev != $File::Find::topdev ) ) && print "MIDDLE SZINPT:\t",($size_input),"\n", && ( int(((-s _) + 1023) / 1024 ) > $size_input ) && push ((@large_files), $name); } } ###-- End sub --### print scalar @large_files;

    __OUTPUT__ MIDDLE SZINPT: 26214400 MIDDLE SZINPT: 26214400 MIDDLE SZINPT: 26214400 0