in reply to Re^2: file is replaced
in thread file is replaced

You only open $fh somewhere down below in your xml_output routine, but use it all over the place. Most likely you're trying to write to $fh before having opened it. Most likely, this when your program reaches line 66.