I'd like to close STDOUT to prevent my code from outputing a particular image that I need for further computation but do not want on my web page.
So i want to close STDOUT, do what I have to do with my code, then reopen STDOUT to output stuff to a web page. (Not to a file)
What I tried is:
close STDOUT;
# my code here
open STDOUT;
This doesn't work...
Thanks
To (re)open STDOUT or STDERR as an in-memory file, close it first:
From the perl doc: http://perldoc.perl.org/functions/open.html You have a : after your close, don't do that. The open above should also work with jus
This thread in perl monks might help you too: http://www.perlmonks.org/?node_id=635010
It's bad to close STDOUT since much assumes it's always open. It's better to redirect it to
/dev/null
(unix) ornul
(Windows).If you want to redirect the file descriptor,
If you just want to redirect STDOUT,
If you just want to redirect the default output handle,
There are several ways to approach your problem, and many of them do not require you to close STDOUT and risk fubaring your program's standard I/O channels.
For example, you can use the (1-arg)
select
command to direct the output ofprint
commands somewhere else temporarily.You can also reassign the
*STDOUT
glob at run-time without closing any handles.You can implement something to catch STDOUT like so:
And then use it like so:
Localizing the filehandle inside stdout_of() allows you to avoid the tricks of closing and re-opening STDOUT.
I checked 2 ways:
select
*OLD_STDOUT = * STDOUT
, and see they are not usable in common case.The reason is these 2 approachs redirect STDOUT only if "print" or something else is used in a Perl Script. But if you use "system()" call or call subscript, their output got to standard STDOUT anyway =((.
My point of view, the indeed solution is to be:
I checked this solution and it worked for me.
Read the documentation for open.
Search for "Here is a script that saves, redirects, and restores STDOUT and STDERR using various methods".
What you want to do is not close STDOUT, but rather redirect it to /dev/null temporarily.