use strict; use Carp; sub readin_dir { my ( $filenames, $paths, $ext ) = @_; if ( ref($filenames) ne 'ARRAY' or ref($paths) ne 'ARRAY' or @$paths == 0 ) { carp( "readin_dir call lacks array_ref(s) for file names and/or paths\n" ); return; } for my $path ( @$paths ) { $path =~ s{/+$}{}; # don't need or want trailing slash(es) opendir( my $dh, $path ) or do { warn "readin_dir: open failed for $path\n"; next }; my @subdirs = (); while ( my $file = readdir( $dh )) { next if ( $file =~ /^\.{1,2}$/ ); if ( -d "$path/$file" ) { push @subdirs, "$path/$file"; } elsif ( $ext eq '' or $file =~ /\.$ext$/ ) { push @$filenames, "$path/$file"; } } closedir( $dh ); readin_dir( $filenames, \@subdirs, $ext ) if ( @subdirs ); } }