Well it's not quite fine, on some OS there is a chance that one could find oneself attempting to delete the current directory and it's parent ('.' and '..'). I would suggest something like:
opendir my $d, $dir;
+
my @f = sort { -M $b <=> -M $a }
map { "$dir/$_" }
grep !/^\.{1,2}$/,readdir $d;
+
unlink @f[-0,1] if @f > 20;
The only problem using the conditional like that on the unlink line is that it makes it more difficult to test whether the unlink succeeded - I would expand it to a full
if block.
/J\