Re: output from cgi
by mrbbking (Hermit) on Jan 08, 2002 at 18:29 UTC
|
...and don't forget to test your system call (open) for success...
open (DAT, ">file.txt") or die "Can't open file.txt: $!\n";
Depending on your application and your users, you might want to write up a prettier error handling routine, instead of relying on a die. In a CGI, die writes the error to the server logs. If you use CGI(FatalsToBrowser) it'll write the plain text of the error to the browser. Good for you during development, probably not good for your users.
But whatever you do, make sure your open call worked before writing to the filehandle. | [reply] [d/l] |
Re: output from cgi
by Gerard (Pilgrim) on Jan 08, 2002 at 17:44 UTC
|
Try this or something similar...
open (DAT, ">file.txt") ;
print DAT $foo;
close DAT;
Hope this helps, Gerard | [reply] [d/l] |
Re: output from cgi
by mce (Curate) on Jan 08, 2002 at 18:58 UTC
|
Hi Lisa,
As mentioned here above, use open.
CGI is very simple and is just an output to STDOUT. If you
want to create an html file from a script, than you should
not print to STDOUT but select another file
descriptor. (and don't print the header).
Than it is just a matter of using
the CGI modules you like.
This is f.e. the bases of this
suberb script.
Just my 0.02 (euro) cents worth.
---------------------------
Dr. Mark Ceulemans
Senior Consultant
IT Masters, Belgium
| [reply] |
Re: output from cgi
by Masem (Monsignor) on Jan 08, 2002 at 19:30 UTC
|
The CGI.pm module itself does not send anything to any specific ports. The output methods of CGI only produce strings, which you have to explicitly tell your code to print out.
Because of this, you can use the standard print FILEHANDLE @text method for outputting any text to a file. FILEHANDLE can be STDOUT, or any file handle that you specified.
If you know you're going to do this up front, then you don't need to worry about detacting STDOUT and attaching it to a different file stream. However, if you are just testing code, then that method will work.
Also remember that any perl code using CGI.pm can also be run from the command line, allowing you to enter any CGI variables that you want, and the output will be back to STDOUT, so that's another possible solution.
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important
| [reply] [d/l] |
Re: output from cgi
by Chrisf (Friar) on Jan 08, 2002 at 18:20 UTC
|
Or if you just want to append the data to an existing file use >>
open (DAT, ">>yourData"); # note the >>
print DAT $foo;
close DAT;
Update: Chris function not no caffeine well.. (ie remember to check the open call as mrbbking said)
Also take a look at This | [reply] [d/l] |
Re: output from cgi
by Erik Hensema (Sexton) on Jan 08, 2002 at 18:57 UTC
|
First, close STDOUT and then reopen it. Totally untested code:
close STDOUT;
open STDOUT, ">file.txt";
| [reply] [d/l] |
|
|
If you need to do this in a module or a larger program, you can localize the filehandle glob, or use the two-argument select. This is handy to avoid spooky action at a distance:
open(OUT, ">firstout.txt") or die "1: $!\n";
print OUT "First\n";
{
local *OUT;
open(OUT, ">secondout.txt") or die "2: $!\n";
print OUT "Second\n";
}
print OUT "Third\n";
Update: If there's an explicit close, it's also worth checking for an error. Someone on p5p had intermittent test failures (creating temporary files) on a nearly full disk. I rarely check, but know I should. | [reply] [d/l] |
|
|
| [reply] [d/l] [select] |