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 = ();
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
