Home
TeamSite
Perl Find Question
System
All of my perl does use strict. I have lots of scripts that need to find assets in a filesystem using various conditions - find all files, find all files containing some text, find all files with filenames matching some string, find all directories, find all files and directories, etc. I use File::Find::find for this but I would like to move the wanted function into a module with some more generic logic (pass a regex, etc.). But with strict on I don't know how to pass the array, regex, etc. to the find wanted subroutine in the module - the Perl compiler throws warnings about undeclared variables and won't let me install the module.
I hope this makes sense but I can clarify if needed. Hopefully someone has already resolved a similar problem.
Find more posts tagged with
Comments
Johnny
Do you want to post the code as an attachment so we can have a look.
Im not sure where it would be complaining about the declaration, but sometimes its a little cleaner to treat your params as a hash ref.
eg $someObj->someMethod(param1 => 'value1',
param2 => 'value2');
and then check to see if they exist/defined in the routine.
Cant really say much exactly without seeing what you are doing.
Also for what it is worth, Ive been using TeamSite:
irwalk instead of file::find.
I find it to be alot better to work with. You can do a few interesting things with it too.
have a read of the perldoc.
John Cuiuli
Migrateduser
Let's say I have a Perl module FindUtil.pm, that contains a subroutine findMatchingFiles intended to work as a File::Find::find "wanted" function.
sub findMatchingFiles
{
my ${file} = ${File::Find::name};
...
}
Then I have a script that I want to have use this wanted function.
use strict;
use FindUtil.pm;
my @{list} = ();
my ${dir} = "/tmp";
find( \&findMatchingFiles, ${dir} );
What I need to know is how do I pass parameters (such as the array to populate or a regex if I only want the array populated with files with names matching some pattern) to findMatchingFiles.
Thanks,
-John
Adam Stoller
It truly would help to see both FindUtil.pm *and* the script that uses it....
--fish
Strolling Prime Minister of no fixed address
Migrateduser
Let's say I have the attached code and it does not generate warnings (though I don't think the global variables are good practice). I want to move findMatchingFiles to a module but then list and regex are undefined. What I want is a way to add my variables to the things that are passed to find.
Migrateduser
I guess I move the global variables into the package, then set the regex before calling find with something like ${FindUtil::regex}, then reference them from the calling script using @{FindUtil::foundList} or something.
Johnny
file::find routine that you pass to is controlled by file:find meaning you cant tack on your own params to it.
also if your routine is in a module you will to export to it so that is is part of the script namespace.
TeamSite:
Irwalk has an custom hash ref called ->aux that lets you put stuff in there so your routine can use them... almost like params in a way.
Its always good to have your find routine to act as a controller which determines which routine should handle the file/directory and what info should be put into ->aux
John Cuiuli
Migrateduser
The llama book (around page 70 in this edition) has some information that may be applicable - when I get a chance I'll give it a try.
Migrateduser
Well here's a first stab.