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

I'm asking myself how to not use global variables for File::Find.

Currently my finds look like this:

my @files; find( sub { push( @files, $_ ); }, @my_directories );
So I push into the global variable, but I don't really like this. I'd love to have local variables here. Is there an easy solution to it I haven't thought of yet?


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re: File::Find and non global Variables
by Corion (Patriarch) on Jul 20, 2006 at 09:48 UTC

    Use a bare block to limit the scope of your variables:

    { my @files; find( sub { push @files, $_ }, @my_directories ); # ... do stuff to @files }; # @files is invisible here
Re: File::Find and non global Variables
by Ovid (Cardinal) on Jul 20, 2006 at 09:54 UTC
Re: File::Find and non global Variables
by davorg (Chancellor) on Jul 20, 2006 at 09:57 UTC

    Could you switch to Find::File::Rule. Lightly adapting the POD would suggest something like this:

    use File::Find::Rule; my @files = File::Find::Rule->file() ->in( @my_directories );
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: File::Find and non global Variables
by Tanktalus (Canon) on Jul 20, 2006 at 13:55 UTC

    Um - @files isn't a global variable. It's a lexical variable as it is defined with my. The anonymous sub that you're passing to find is closed on the lexical @files. Thus, your example already is how to avoid using globals with File::Find.