in reply to Re: File::Find: Return array of "wanted" files
in thread File::Find: Return array of "wanted" files

Thanks.

FWIW, I was using fgrep from File::Grep. I should have included the "use" lines for clarity.

I actually didn't realize that I can push to @found from within the sub. Guess I better read up some more on scope..

  • Comment on Re^2: File::Find: Return array of "wanted" files

Replies are listed 'Best First'.
Re^3: File::Find: Return array of "wanted" files
by Laurent_R (Canon) on Oct 04, 2013 at 17:14 UTC

    The scope of a lexical is going from where the lexical is declared to the end of the enclosing block. In the case of @found above, to the end of the file.

      Ah... so if I wanted to push from the sub to the array inside the loop, I would have to declare the array outside of the loop and reinitialise it with every iteration? Or declare the sub inside of the loop?

        Yes, in such a case, you want to declare the array before entering the loop. That's what choroba is doing in the suggested code, declaring @found right before entering the loop:

        my @found; sub findstuff { my $file = $_; return unless -f $file; my $fullpath = $File::Find::name; if (fgrep(qr/regex/, $file)) { push @found, $fullpath; } }