• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

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

I closed STDOUT with "close STDOUT;"
How do I reopen STDOUT?   I am programming Perl on the Mongoose WebServer.

0
rgbcof
Asked:
rgbcof
  • 2
2 Solutions
 
mrjoltcolaCommented:
It depends on where you want STDOUT to point to.

By default on a regular Perl program, STDOUT (and STDIN and STDERR) are pre-initialized to the standard OS file handles (usually 0, 1 and 2). It is actually platform dependent to reopen those after you close them, so if you have need in a program to close STDOUT, but evetually want to reopen the console STDOUT, then you need to copy (dup) STDOUT to another filehandle before closing it, so you can dup it (reopen) later.

Now if you are running CGI, STDOUT points to the web server process, but the concept is still the same. Completely closing it will lose it.

You CAN open STDOUT to an arbitrary file easily, however. If you just want STDOUT to go to some log file, you can do this:

 
open(STDOUT, ">>stdout.log") or die $!;

Open in new window


But perhaps you can describe in more detail what you are trying to accomplish.
0
 
ZarabozoCommented:
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
0
 
ZarabozoCommented:
Sorry, the second example should've been this one:

my $err;
eval {
	local *STDERR;
	open STDERR, '>>', \$err;
	# Some process here that I don't want to actually go to STDERR
	warn "This is a warning\n";
	die "There was a fake problem here";
};
print "Checking the result of the eval call:\n";
if ($@) {
	print "CHECK: Ok, I'm not dead, but there was a problem:\n";
	print "STDERR: $err";
	print "STDERR: $@";
} else {
	print "Everything Ok\n";
}
print "Continue working\n";

Open in new window

0
 
rgbcofAuthor 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now