in reply to How to write to a file using CGI
Maybe this helps you with debugging:
print "Writing record ... "; print FH "record is [$record]\n"; print "Wrote record: [$record]"; [download]