in reply to removing a needle from a haystack

grep is not the best choice for finding a single match in a large array; unfortunately Perl doesn't offer a function that is as simple as grep for this, so people use it anyway.

Depending on the data, a for loop that bails out at the first match is usually faster. Since in this case we want to avoid the readdir calls though, a while is best:

opendir(DIR, $path) or die("No opendir on $path\n"); while (defined($_ = readdir DIR)) { if (/^${id}\_.*$/o) { $filename = "$path/$_"; last; } } closedir(DIR); if ($filename) { unlink($filename) or die("No unlink on $filename\n"); }