Search code examples
perlfindglob

Efficiency of Finding Files with Perl


I'm trying to get an array of files from a directory tree in a Perl script. Sometimes I can grab them with glob expansion, but something I only way to capture what I need is with a regex.

For example, I might want to get all of the files which would match verify/*.finished with shell expansion. Using glob(<pattern>) is faster than matching everything found with File::Find when I know the depth at which the "verify" directory lives (e.g. glob("*/*/*/verify/*.finished"), but am a little stuck when I need to rely on regex matching.

Is there any way to get the efficiency of glob with the flexibility of regex?


Solution

  • Well, you could just generate the full list of files with glob, then grep the results using regular expressions:

    my @files = grep { /\.finished\z/ } glob '*/*/*/verify/*';
    

    EDIT:

    If the question is if there's a facility that works like glob but uses regular expressions, I believe the answer is no. In the completely general case, I don't see that you have any alternative but to traverse an entire directory tree, and I doubt you'll be able to do substantially better than File::Find.