use Path::Iterator::Rule; ... my $res_files = list_dirs($dir,$datanames); sub list_dirs { my ( $locations, $fnames ) = @_; # assumes each is a simple arrayref my $finder = Path::Iterator::Rule->new; $finder->name( @{ $fnames } ); my $get_next_match = $finder->iter( @{ $locations } ); my @found_filenames; while ( defined ( my $match = $get_next_match->() ) ) { push @found_filenames, $match; } return \@found_filenames; }