in reply to output from cgi

Try this or something similar...
open (DAT, ">file.txt") ; print DAT $foo; close DAT;

Hope this helps,
Gerard