Smith Jeff D wrote:

> I am having some difficulty figuring out how to do something like the
> following, where I use File:Find to find a list of filenames that are
> then used
> 
> To create a hash of filenames that I can pass back to the calling
> subroutine for use elsewhere. 
> 
> I can get the find to work, the loading of the hash of filenames, but
> can't seem to figure out to pass a reference to this hash back to the
> calling function. It must
> 
> be easy but all the examples I've read about either just print the hash
> or store it in a file--I'd prefer not to create another file.  What
> stupid thing am I missing?
> 
> Thanks in advance:
> 
> ......stripped down version follows---
> 
> ------------
> use strict;
> use warnings;
> 
> use File::Find;
> use File::Basename;
> 
> getfiles();
> 
> sub getfiles {
> 
> print "START THE GETFILES  ENGINE\n";
> my  $hashref = undef;
> my $searchdirectory = 'C:/temp';
> print "Searchdir is $searchdirectory \n";
> 
> #find(\&wanted, $searchdirectory);
> 
> # The find wanted seems to work as intended but the return doesn't work---
> 
> $hashref  = find(\&wanted, $searchdirectory);
> # value is always zero, never a hash reference so I always
> # get a "Can't use string (")") as a HASH ref while "strict refs" in use
> at snippet.pl line 20"
> 
> # This never executes because of the above error-- $hashref never gets
> the hash reference from the return:
> while ((my $getkey, my $getvalue) = each %{$hashref} ) {
>         print "$getkey -> $getvalue \n";
> }
> print "END OF GETFILES\n";
> } # end of getfiles
> 
> sub wanted {
>        
>        
>         my %newhash = ();

Move %newhash out of wanted to make it global.  I know of no way to
otherwise maintain state across calls to wanted.  It would be nice if
you could pass an arg to wanted along with the args it currently gets.

Another way you could try is have File::Find pass the arg for you.
This seems to work for me:

use strict;
use File::Find;

my %hash = ();

find( { wanted => \&wanted, myarg => \%hash }, '.');

sub wanted {
        my $hashref = $_[0];

my $href = $hashref->{myarg};           # get my hash arg

foreach (keys %$href) {
        print "myarg: $_ => $href->{$_}\n";     # print hash for debug
}
print "\n";

$href->{$File::Find::name} = -s $File::Find::name;      # add size of this file

}

__END__



>         if ( -f) {
>                 my $filename = basename($_) if -f;
> #               print "$filename and ".dirname($_)." \n";
>                 $newhash{$filename} = "TESTVALUE";
>                 while ((my $newkey, my $newvalue) = each %newhash) {
>                         print "$newkey - > $newvalue \n";
>                 }
>         return \%newhash;
>         }
> } # End of wanted
> 


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to