chdir $the_directory or die "Couldn't chdir to $the_directory: $!"; opendir(D, ".") or die "Couldn't open . ($the_directory): $!"; while ($file = readdir D) { next unless $file =~ /\.las$/; # other logic later... } closedir D; #### chdir $the_directory or die "Couldn't chdir to $the_directory: $!"; opendir(D, ".") or die "Couldn't open . ($the_directory): $!"; my @files = grep { /\.las$/ } readdir D; closedir D; foreach my $file (@files) { # whatever... }