[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-04-21
4
Medium Priority
?
608 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.

0
Comment
Question by:rgbcof
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 1000 total points
ID: 35440633
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
 
LVL 2

Expert Comment

by:Zarabozo
ID: 35448232
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
 
LVL 2

Assisted Solution

by:Zarabozo
Zarabozo earned 1000 total points
ID: 35448250
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
 

Author Closing Comment

by:rgbcof
ID: 35448547
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question