Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I could not figure out how to return file names that I have found using find command and subroutine. I would like to use local variables for that if possible. Is there any way to do it Thanks, Nevzat
  • Comment on Returning array of file names form find using local variables?

Replies are listed 'Best First'.
Re: Returning array of file names form find using local variables?
by strat (Canon) on May 07, 2002 at 09:04 UTC
    Hello,

    that is not so easy, because File::Find seems to rely on global variables or closures for returning values. One way might be:

    use File::Find (); # no namespace pollution my @startDirectories = ....; my @foundFiles = &FindFiles(@startDirectories); sub FindFiles { my @rootdirs = @_; my @foundFiles = (); File::Find::find sub { if (-f $File::Find::name){ # just files push (@foundFiles, $File::Find::name); } }, @rootdirs; return (@foundFiles); } # FindFiles
    Haven't run this code, but hope that it will work.

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

      mr. strat, thanks for the information. I have used the exactly what you have coded. I think that push command is the key. If I get back to you Mr. Hotshot comment, I am not really fond of global variables, just a skeeing the way to avoid it, if not then I will use what has been proposed. Thanks for your time and attention. Regards, Nevzat
Re: Returning array of file names form find using local variables?
by hotshot (Prior) on May 07, 2002 at 09:05 UTC
    Can you spread us more details. the find command is in this function you mentioned? and why you insist on local variable?

    Thanks.

    Hotshot
      Global variables are generally seen as a bad thing, and should be avoided. When you 'use strict;' you must declare all of your local variables with 'my' as discussed in this node.