Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Catch all die in Perl?

by Anonymous Monk
on Jul 18, 2005 at 16:07 UTC ( #475869=perlquestion: print w/replies, xml ) Need Help??

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

Great Monks,

I'm just about finished writing some automatic error logging and emailing modules for my department's scripts. I'm just thinking about how I can easily apply these to all the existing scripts without using or mail(@args); every time I want to check for an error. I would like to have it so that if any kind of error at all occurs when running the script, then the email will get sent and the error will get logged.

So is there an all encapsulating error checker in Perl? I heard "try" is now in Perl or maybe something with __DIE__? I'm pretty new to the language so thanks for the help!


Replies are listed 'Best First'.
Re: Catch all die in Perl?
by friedo (Prior) on Jul 18, 2005 at 16:21 UTC
    You can install a custom handler in %SIG which will catch all fatal errors:

    BEGIN { $SIG{__DIE__} = sub { mail( $_[0] ) }; }

    That will send the die message to your mail routine. Alternatively, if you don't want to catch all dies, check out the block form of eval.

      Thanks a ton, that code chunk is just what I was looking for!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2019-11-13 07:07 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (69 votes). Check out past polls.