Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Tidying up after redirecting STDERR

by davies (Prior)
on Feb 22, 2010 at 20:33 UTC ( #824699=perlquestion: print w/replies, xml ) Need Help??

davies has asked for the wisdom of the Perl Monks concerning the following question:

I have written some Perl that generates POD and then uses pod2html to convert it. This outputs text that I wish to capture to a text file. pod2html writes to STDERR, and Losedows's redirection fails to pick it up.

Googling gave me http://www.perturb.org/display/entry/883/ with methods of redirecting STDERR. The first failed, but the second, *STDERR = *STDOUT, works perfectly. So my code works. This comes right at the end of my code, so perhaps I shouldn't care about anything else.

But mummy always told me to tidy up after myself (I never do in real life). I don't know if I am building traps for myself by failing to reset STDERR to its normal destination, and advice on that would be appreciated. Since I can't easily tell the two apart from the command prompt, I don't really how to be sure if anything I put in has worked. Nor has my googling given any clues. So, my friends, please could you tell me:
Will Perl reset STDERR automatically, and if so, when?
How do I reset it explicitly?

Regards,

John Davies

Replies are listed 'Best First'.
Re: Tidying up after redirecting STDERR
by ikegami (Pope) on Feb 22, 2010 at 20:49 UTC

    *STDERR = *STDOUT; is transparent to the OS. It just plays with Perl variables. That's why it doesn't survive an exec, for example. As such, this change does not survive perl.

    Even if you did play with the actual file descriptors (>&), file descriptors are process-specific, so the change would still not survive perl.

    Either way, nothing needs to be cleaned up before perl exits.

Re: Tidying up after redirecting STDERR
by JavaFan (Canon) on Feb 22, 2010 at 20:38 UTC
    Will Perl reset STDERR automatically, and if so, when? How do I reset it explicitly?
    Not if you just do *STDERR = *STDOUT. But you can do:
    { local *STDERR = *STDOUT; ... code ... }
    But if this happens at the end of the program, why does it matter?
Re: Tidying up after redirecting STDERR
by crashtest (Curate) on Feb 22, 2010 at 22:08 UTC

    Since I can't easily tell the two apart from the command prompt, I don't really how to be sure if anything I put in has worked.
    Tell apart STDOUT and STDERR? Just redirect them to separate files:
    perl yourscript.pl >stdout.txt 2>stderr.txt

    Although I feel local'izing the file handles is cleaner, as suggested by JavaFan, you could alternatively store them. Then re-store them. That way, you'd have STDERR available in the block if you needed it.

    my ($out, $err) = (*STDOUT, *STDERR); eval{ *STDERR = *STDOUT; # pod2html code here # ... }; *STDOUT = $out; *STDERR = $err;

Re: Tidying up after redirecting STDERR
by davies (Prior) on Feb 22, 2010 at 23:17 UTC
    Many thanks for the replies; they've given me a lot to think about. The reason I'm concerned about what happens at the end of my code is hubris, pure and simple. My code is intended for public consumption, and as I am collecting the output and processing it further, it occurred to me that someone else might want to do the same sort of thing in Perl, and that by leaving STDERR in an unpatriotic state might get me more curses than plaudits. I'm going to have to play around before I decide which way to go, but I don't feel anything like as scared as I did a few hours ago!

    Thanks again,

    John Davies

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://824699]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2019-10-18 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?