in reply to Reading Multiple Directories

Another way to get a list of files is to use grep and -f:
use warnings; use strict; use Data::Dumper; use File::Slurp qw(read_dir); my @files; my @directories = ( '/dir1', '/dir1/imgs' ); for my $d (@directories) { push @files, grep { -f "$d/$_" } read_dir($d); } print Dumper(\@files);
Also, File::Slurp automatically excludes the special dot directories (. and ..) for you. There is no need for you to explicitly get rid of them.

Replies are listed 'Best First'.
Re^2: Reading Multiple Directories
by Anonymous Monk on Aug 25, 2011 at 15:52 UTC
    I like this way better, it seems to have less over head or code any way, but I need to do one more test before "pushing" the files into @files. Test for file size, trying this way, but not really sure if its efficient.
    use warnings; use strict; use Data::Dumper; use File::Slurp qw(read_dir); my @files; my @directories = ( '/dir1', '/dir1/imgs' ); for my $d (@directories) { push @files, grep { -f "$d/$_" } read_dir($d); foreach my $file(@files) { my $filesize = -s $d.$file; #need to check if file is > then 0 and less then 2M; } }
    Thanks once again!