Reputation: 7705
I want to (by using File::Find) first list all files in current directory, and after that jump to a subdirectory. Is it possible?
Upvotes: 3
Views: 1765
Reputation: 98398
Use the preprocess option to do the files in each directory before descending into subdirectories:
use strict;
use warnings;
use File::Find 'find';
find(
{
'wanted' => sub { print "$File::Find::name\n" },
'preprocess' => sub { sort { -d $a <=> -d $b } @_ }
},
'.'
);
Though to avoid extra stats, it should be:
sub { map $_->[0], sort { $a->[1] <=> $b->[1] } map [ $_, -d $_ ], @_ }
Upvotes: 8
Reputation: 9697
There is preprocess callback that is called when directory is entered. It can be used for the task like this:
use File::Find;
my $directory = '.';
find({
wanted => sub {
# do nothing
},
preprocess => sub {
print "$File::Find::dir :\n", join("\n", <*>),"\n\n";
return @_; # no filtering
},
}, $directory);
It prints current directory name and list of files within. Note that preprocess
is given all directory entries for filtering and they need to be returned.
Upvotes: 1