while ( readdir $dirhandle ) { if ( -f $_ ) { if ( open $file{ $_ }, '<', $_ ) { warn sprintf q{I have %d files open from the working directory.}, scalar keys %file; # do stuff } else { delete $files{ $_ }; # report error } } } my @files = keys %file; for ( @files ) { # do something that for some reason requires all the files in the directory open at once } for ( @files ) { close $file{ $_ }; delete $file{ $_ }; }