Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: output from cgi

by Erik Hensema (Sexton)
on Jan 08, 2002 at 18:57 UTC ( [id://137137]=note: print w/replies, xml ) Need Help??


in reply to output from cgi

First, close STDOUT and then reopen it. Totally untested code:

close STDOUT; open STDOUT, ">file.txt";

Replies are listed 'Best First'.
Re: Re: output from cgi
by chromatic (Archbishop) on Jan 08, 2002 at 21:14 UTC
    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.

      If I could suggest: an explicit close ( OUT ); statement after the print statement, even if going out of scope does close the local OUT file handle.

      --t. alex

      "Excellent. Release the hounds." -- Monty Burns.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://137137]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found