in reply to print out certain extension files

You basically have two nested loops on files: one with the while (my $file = readdir DIR), which reads each file entry in turn, and, then, again with the foreach (@files). Keep only one of them.