in reply to Re^2: file modifications using file::find
in thread file modifications using file::find
G'day Marshall,
"Ken, I like your code!"
Thanks. I appreciate the compliment.
'You snuck in the tests like "-z _".'
Unfortunately, the OP gave very little information about Perl experience or, indeed, File::Find usage requirements. As this was a first post, I didn't make a big deal about it; although, I did provide a link "for information on how you can help us to help you".
My main aim was to provide the requested "help or a tutorial". I did consider including an explanation for the special filehandle, '_'; however, as I didn't know if these tests would be used, I chose to add a link to that information. I did use the link text "file tests", in case "-X" was too cryptic. :-)
"About cwd"
The last paragraph of my post did include a warning about specifying directories; an explanation that I'd only used Cwd for my example code; and, suggested a number of better alternatives. Furthermore, I only used the getcwd() function to generate the required @directories_to_search for find(); there was no explicit changing of directories in my script.
Of course, there is the implicit changing of directories as part of File::Find's default behaviour. You can change that: see "File::Find - %options - no_chdir".
Your comments regarding using an array are sound. You may actually want to use the list more than once. I assume you know how to do this, but for the OP or anybody else, here's a very basic (partial) code example:
... find(\&wanted, @dirs); do_something_with(get_found_files()); check_something_done_with(get_found_files()); ... { my @found_files; sub get_found_files { return @found_files } sub wanted { ... push @found_files, $File::Find::name; ... } }
Note that the anonymous block makes @found_files (lexically) private: only get_found_files() and wanted() have access to it. You can, of course, modify the list returned, but the original will stay intact. For anyone unfamiliar with this concept, see "perlsub: Private Variables via my()" for a more in-depth discussion of this topic.
— Ken
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: file modifications using file::find
by Marshall (Canon) on Jul 26, 2021 at 01:21 UTC |