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

Is it possible to have File::Find stop as soon as the first match is found, instead of recursing through all the directories?

Replies are listed 'Best First'.
Re: stopping File::Find at the first match
by moritz (Cardinal) on Mar 31, 2009 at 22:38 UTC
    I'm sure it's possible. My first attempt would be to throw an exception in your callback and catch it on the outside.
      or even goto :)
Re: stopping File::Find at the first match
by repellent (Priest) on Mar 31, 2009 at 22:54 UTC
    Not exact but perhaps a global $prune would do the trick. Untested:
    my $global_prune = 0; sub wanted { $File::Find::prune = 1 and return if $global_prune; $global_prune = 1 if /first match/; }
Re: stopping File::Find at the first match
by locked_user sundialsvc4 (Abbot) on Mar 31, 2009 at 22:48 UTC

    Since the CPAN documentation expressly states that the return-value of the wanted function is ignored, I surmise that “throwing an exception” is the proper way to do this.

    But I do not claim to speak from authority on this...

Re: stopping File::Find at the first match
by CountZero (Bishop) on Apr 01, 2009 at 06:10 UTC
    May be this is the time to switch to File::Find::Rule which has a match operator which returns after finding each file you search for?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James