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

Re: Catching STDERR

by mr.nick (Chaplain)
on Jun 27, 2001 at 04:36 UTC ( [id://91792]=note: print w/replies, xml ) Need Help??


in reply to Catching STDERR

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 ...

Replies are listed 'Best First'.
Re: Re: Catching STDERR
by kiseok7 (Beadle) on Jun 27, 2001 at 04:46 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-07-24 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.