opendir my $cwd_dh, '.' or die "Can't opendir: $!"; while ( my $file = readdir $cwd_dh ) { next unless $file =~ m{ \. imap \z }xms; # ... } closedir $cwd_dh;