This looks like a bug in the test - in the block where $x is set,
uncommenting the 'warn' line shows that a directory 'foo' still exists.
But the section where @files is set will ignore directories.
If readdir returns the directory first, then $x will be set to 'foo',
and none of the other files will match that basename. (In fact, all the
other files will get deleted - 'foo' always appears last after the list
has been truncated, but in this case would also be $files[0].)
Probably filesystem-dependent? No CPAN testers have picked this up.
--
Tim Retout <tim at retout.co.uk>