tags:

views:

148

answers:

2

Greetings ,

I can close the STDERR in perl using;

close(STDERR)

and after executing some logic , I want to open it back again. How can I do it?

I tried

open(STDERR,">&STDERR");

and didn't work.

Thanks in advance.

+10  A: 

Why do you want to close STDERR?

You could put it aside...

open(FOO, ">/dev/null"); # or ">nul" on Windows
*TEMP = *STDERR;
*STDERR = *FOO;
... then
*STDERR = *TEMP;
pascal
File::Spec->devnull() will return the appropriate filename
ysth
+6  A: 

dup it first, then dup the dup to reopen it (error checking left as an exercise for the reader, though dealing with errors when STDERR is unavailable can be an exercise in frustration):

open(my $saveerr, ">&STDERR");
close(STDERR);
open(STDERR, ">&", $saveerr);

Note that when you close STDERR you free file descriptor 2; if you open another file and it gets file descriptor 2, any non-Perl libraries you are using may think that other file is stderr.

ysth