in reply to Strange Error

You could try the glob() function instead of filtering the contents of the directory yourself, like this:

my @files = glob("*.txt");