We help IT Professionals succeed at work.

perl, How do you re-open STDOUT after closing.

rgbcof
rgbcof asked
on
662 Views
Last Modified: 2012-05-11
I closed STDOUT with "close STDOUT;"
How do I reopen STDOUT?   I am programming Perl on the Mongoose WebServer.

Comment
Watch Question

Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
I would ask you, why do you need to close it in the first place?

If you need to stop the output at some point, you can do something like this:

my $handler;
local *MYOUTPUT;
open *MYOUTPUT, '>>', \$handler;
my $stdout = select *MYOUTPUT;
print "This is not going to STDOUT, is going to the handler var\n";
print "This too\n";
select $stdout;
print "This should go out normally\n";
print "And, this is what I received in the handler var:\n";
print $handler;

Open in new window


Another way to control the output (e.g. for STDERR), is using an eval. It will be automatically restored one you leave the eval block:

my $handler;
local *MYOUTPUT;
open *MYOUTPUT, '>>', \$handler;
my $stdout = select *MYOUTPUT;
print "This is not going to STDOUT, is going to the handler var\n";
print "This too\n";
select $stdout;
print "This should go out normally\n";
print "And, this is what I received in the handler var:\n";
print $handler;

Open in new window


Let me know if this helps. Thanks.

Francisco
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
mrjoltcola - thanks for the wonderful explanation
Zarabozo - yorur example is awesome.

I was having an autoflush problem with the webserver, and was trying to come up with a work around.  I have tried many ways to autoflush in Perl, but nothing works.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.