my $pattern = join '|', @filetypes; ... while (readdir($eh)) { next if /~$/ || -d || !/$pattern/i; ...