Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

redirecting STDERR, then back again

by melguin (Pilgrim)
on Aug 30, 2001 at 03:06 UTC ( [id://108942]=perlquestion: print w/replies, xml ) Need Help??

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

I got this program I've been handed to make work right. What I need to do is redirect STDERR in the middle of it to someplace else, the a little while later redirect it back.

I remember reading about it somewhere but haven't been able to find it again. Any ideas or pointers to references?

as always, many thanks,

melguin.

Replies are listed 'Best First'.
Re: redirecting STDERR, then back again
by danger (Priest) on Aug 30, 2001 at 03:14 UTC

    The documentation for open() perlfunc:open has an example of saving, redirecting, and restoring STDOUT and STDERR that you might find helpful.

Re: redirecting STDERR, then back again
by grinder (Bishop) on Aug 30, 2001 at 14:45 UTC

    The trick is to use a local'ized version of STDERR, in the scope that interests you:

    #! /usr/bin/perl -w use strict; warn "initial write to STDERR\n"; # open a scope { local *STDERR; open STDERR, '>tmp.stderr' or die "Cannot open tmp.stderr for output +: $!\n"; warn "redirected write to STDERR\n"; close STDERR; } warn "final write to STDERR\n";

    You could probably get away without having the close STDERR at the end of the scope, but it's much cleaner (easier to understand what's going on) to do so explicitly.

    Later: I just went and checked the example as per danger's suggestion. The technique there seems to be a little more complicated. You basically open up another file handle to cache a copy of STDERR, and then when you are finished you use that to reset the original STDERR back again. Which means there's some baggage you have to drag along with you (the previous STDERR descriptor). Using local hides this for you => one less damned thing to go wrong.

    --
    g r i n d e r
      Using local hides this for you => one less damned thing to go wrong.

      Unfortunately, copying file descriptors using symbol table globs doesn't work very well with the underlying IO subsystem.

      You're code reopens Perl's STDERR but the original STDERR is hanging around, hidden by local. This means the file descriptor 2 is left open so that your new Perl STDERR won't be "stderr" to C routines nor to child processes. So several things that would work as expected if you used the more complicated method recommended by the documentation won't work with your method.

              - tye (but my friends call me "Tye")
Re: redirecting STDERR, then back again
by princepawn (Parson) on Aug 30, 2001 at 03:15 UTC
    search for the first part of your question by typing "dup STDERR" into the search box.

    I presume that if you saved a copy of the filehandle you wanted to restore:

    my $restore = \*original;
    that you could use the dup method discussed in the posts you will find to restore it.
Re: redirecting STDERR, then back again
by mrmick (Curate) on Aug 30, 2001 at 16:26 UTC
    You might also use select in a local block to temporarily point the STDERR filehandle someplace else.

    NOTE: I've done this with STDOUT but not with STDERR (yet).

    Mick
Re: redirecting STDERR, then back again
by melguin (Pilgrim) on Aug 31, 2001 at 01:32 UTC
    Here's what ended up working:
    open(OLDERR, ">&STDERR"); open(STDERR, ">>/tmp/tmp.spderr") or die "Can't dup stdout"; select(STDOUT); $| = 1; # make unbuffered print OLDERR ""; #this fixed an error about OLDERR not being used ## do my stuff here. close(STDERR); open(STDERR, ">&OLDERR");
    As danger said, this is in perlfunc:open.

    thanks,

    melguin

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-19 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found