Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Reset STDERR to console

by saintex (Scribe)
on Apr 01, 2011 at 09:33 UTC ( #896823=perlquestion: print w/ replies, xml ) Need Help??
saintex has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, after redirect STDERR in a file, like so:
open STDERR, >>,'myLog.log';

I would like to re-have STDERR in my console, before the end of the script.

How can I do that?

Comment on Reset STDERR to console
Download Code
Re: Reset STDERR to console
by sumeetgrover (Scribe) on Apr 01, 2011 at 09:52 UTC

    As you'd know, by default, STDERR points to STDOUT.

    After you have manipulated the file, as mentioned in the code above, you can point STDERR back to STDOUT using the following code:
    *STDERR = *STDOUT;

    Hope this helps!

    Sumeet Grover.
      As you'd know, by default, STDERR points to STDOUT.

      Oh really?

      $ perl -le 'print STDOUT foo' 1>/dev/null $ perl -le 'print STDERR foo' 1>/dev/null foo

      So, not true.

      The solution to the original question involves saving the old STDERR in a variable before reopening it. If I remember correctly, perlopentut talks about that.

        Indeed!

        May I remind you that you are 'explicitly' redirecting STDOUT to /dev/null. What if you didn't do that? Would it "really" point to /dev/null?

        Sumeet Grover.
        It would have been accurate to say that the IO handles for STDERR and STDOUT are assigned to the same device rather than that one IO handle is assigned to the other.

        One world, one people

Re: Reset STDERR to console
by educated_foo (Vicar) on Apr 01, 2011 at 10:28 UTC
    The safest thing to do is probably to save your old STDERR and restore it afterwards:
    { local (*SAVEERR); open SAVEERR ">&STDERR"; # ... do your stuff open STDERR, ">&SAVEERR"; }
    Also, take a look at perlopentut.
Re: Reset STDERR to console
by sundialsvc4 (Monsignor) on Apr 01, 2011 at 17:05 UTC

    I am not sure that you can read from STDERR while it is being written to, and of course if (say) it was being directed to a pipe or to /dev/null you couldn’t read it anyhow.

    So, what if we try this ...   In a shell script or somesuch, invoke your Perl program, taking care to put STDERR specifically to somewhere you can capture it.   Capture STDOUT, separately, in the same way.   When the Perl program ends, the shell script now cats (or types) those two streams into its own (the shell script’s...) STDOUT and/or STDERR as appropriate.   The shell script is now simply a “wrapper,” and the job is done.

    It could be very simple ... like this sketch of an idea:

    perl ... your_program_name ... 1>/tmp/stdout 2>/tmp/stderr cat /tmp/stdout cat /tmp/stderr rm /tmp/stdout /tmp/stderr

    No, “the job wasn’t done in the way” that you might now be considering, but ... “the job is done.”   It’s clean, and obvious, and very easily changed.   Win32 and Unix/Linux implementations of the same idea are equally trivial.

Re: Reset STDERR to console
by wind (Priest) on Apr 03, 2011 at 09:10 UTC

    Look at open, search for "OPEN STDERR". There you'll find documented ways to redirect STDOUT and STDERR and later restore them.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://896823]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-08-20 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (118 votes), past polls