First, close STDOUT and then reopen it. Totally untested code:
close STDOUT; open STDOUT, ">file.txt"; [download]
In reply to Re: output from cgi by Erik Hensema in thread output from cgi by wolverina