in reply to Failed to open file for write

Hi,

you've found your problem and its solution by now, but just two debugging tips that may help you in the future.

#Open the output file for saving the results; open my $fh_out, '>', $write_dir.'/'.$filename or die "failed to open '$filename' for write";
Your error message could be improved by replacing the bare filename with the full name with path, and also by printing the $! special variable which will tells you the error reported by the operating system. So, something like this:
my $full_filename = $write_dir.'/'.$filename; open my $fh_out, '>', $full_filename or die "failed to open $full_filename for write $!";
would probably have led you to find the error by yourself.