opendir (DIR, $filepath) || die("Cannot open directory"); should be modified to opendir (DIR, $filepath) || die("Cannot open directory '$filepath': $!");
Similarly, print "$file does not match\n"; could be modified to print "'$file' does not match '$subname'\n";