Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Catching STDERR

by kiseok7 (Beadle)
on Jun 27, 2001 at 04:32 UTC ( #91790=perlquestion: print w/replies, xml ) Need Help??

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

How can I write STDERR to file in perl.

like this :
print STDERR "script start\n"; do_something or die "error in something"; print STDERR "script end\n";
I would like to write "error in something" on file(error.log)

Replies are listed 'Best First'.
Re: Catching STDERR
by mr.nick (Chaplain) on Jun 27, 2001 at 04:36 UTC
    Simple. Just open a file with the filehandle of STDERR:
    #!/usr/bin/perl use strict; open(STDERR,">>error.log") || die "Couldn't redirect STDERR: $!\n"; print STDERR "This goes to the file.\n"; warn "As does this."; die "And this does, too!\n"; close STDERR;

    Update: If you want to restore the original filehandle, then you'll have to do something like this:

    open(OLDERR,">&STDERR") || die "Couldn't dup STDERR: $!\n"; open(STDERR,">>error.log") || die "Couldn't redirect STDERR: $!\n"; print STDERR "This goes to the file.\n"; warn "As does this."; open(STDERR,">&OLDERR") || die "Couldn't restore STDERR: $!\n"; warn "This goes to the screen.";
    Btw, this is almost entirely taken from perldoc -f open.

    mr.nick ...

Re: Catching STDERR
by RhetTbull (Curate) on Jun 27, 2001 at 07:12 UTC
    The aforementioned methods will work great if you want to redirect STDERR to a file. However, if you want to redirect it to a file AND still display it on the STDERR device (e.g. the screen) you might try Filter::Handle. It will let you attach a sub to a file handle (including STDERR) so that you can print the output to a file or filter it any way you like in addition to printing to the terminal. Here's a little snippet to get you started.
    use warnings; use strict; use Filter::Handle qw/subs/; open (LOGFILE, ">logfile") || die "could not open logfile"; #filter STDERR through an anonymous sub Filter \*STDERR, sub {local $_ = "@_"; print LOGFILE "Filtered: $_ "; +$_}; #prints to both STDERR and to LOGFILE print STDERR "error!\n"; #STDERR will no longer be filtered through your sub UnFilter \*STDERR; close LOGFILE;
(Ovid) Re: Catching STDERR
by Ovid (Cardinal) on Jun 27, 2001 at 04:39 UTC

    I can't guarantee the robustness of this solution, but trying opening STDERR for writing:

    use strict; use warnings; open STDERR, "> somefile.txt" or die $!; die "We're dead!";

    This works for me, but no guarantees as I've never tried to redirect STDERR.


    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Just a little FYI: Ovid's doubts are unfounded as this is the most reliable way of catching STDERR. I clicked to this thread fearing I'd see lots of advice to use $SIG{__DIE__}, eval, CORE::GLOBAL::die, tie, pipe-and-fork... A simple open is much preferred. (:

              - tye (but my friends call me "Tye")

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2023-03-26 15:46 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.