Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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?

Replies are listed 'Best First'.
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 (Abbot) 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.

Re: Reset STDERR to console
by sumeetgrover (Monk) 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:

    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.

        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

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-05-07 23:07 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (95 votes). Check out past polls.