in reply to File rewrite question

May I suggest
use CGI::Carp 'fatalsToBrowser'; # at the top of your script ... open(my $RESULTS_FILE...) or die "Error opening results file: param $r +eturned_param, data $data, error $!\n";
Also, it would be nice to see what your parameters actually look like. What happens if you open just one file outside the loop and print "data $data, param $returned_param\n" to it?