in reply to opening new file in different directory

How about one more layer of error checking, on the chdir command:
chdir "/home/main/sub/newfiles" or die $!; open(WRITE, ">$file") or die "Can't open $file: $!";
That is, just because the program runs, doesn't mean it's working correctly. It's possible that the chdir fails and the open is creating a file in the current directory. It's worth checking, anyway...

buckaduck