Reputation: 1256
I have to search a large directory structure of files with File::Find::Rule (or similar).
For testing purposes I'd like top stop after a certain ammount of found files.
I've used ->exec()
with state
or global scope variables, but i haven`t found a way to quit the searching without the use of Labels... Is there a way to do that elegantly?
Upvotes: 2
Views: 96
Reputation: 40778
One way to abort the search, is by calling die
from the exec
handler, then use eval
to catch the exception in the outer scope. For example:
use feature qw(say);
use strict;
use warnings;
use File::Find::Rule;
my $count = 0;
my @files;
eval {
File::Find::Rule->new->file
->exec( sub { die if $count++ > 10; push @files, $_[2]; return 1; })->in('.');
};
if( $@ ) {
say "Aborted search after ", $count - 1, " matches";
}
Upvotes: 4