note
Adam
[autark] makes the excellent [id://42379|suggestion] to use $SIG{__DIE__}. The die signal, AKA <i>sigdie</i>, can be caught and handled by a user defined function.
This is done by adding the handler to the %SIG associative array. (read <A HREF="index.pl?node_id=403#item__SIG">%SIG</A>)
I've spent plenty of time playing with this device, and I thought
I might take a moment to point out a few of the pitfalls. Many of these
are mentioned in the documentation for [die], [warn], [eval], <A HREF="index.pl?node_id=403#item__S">$^S</A>,
and for <A HREF="index.pl?node_id=403#item__SIG">%SIG</A>. (The last two are parts of [perlvar].)
So then, on to the pitfalls.<P>
<P><B>Pitfall 1:</B> While [eval] traps the <i>fatality</i> of sigdie, it does not
block the $SIG{__DIE__} handler from being called.</P>
Solution 1: If you don't want your handler to be used by any
[eval] block, then check <A HREF="index.pl?node_id=403#item__S">$^S</A> at the begining.<BR>
Solution 2: If you have a particular [eval] block where you don't want the
handler to be invoked, then turn it off using [local]:
<code>
sub Something
{
local $@; # Don't step on other code.
# try
eval{
local $SIG{__DIE__}; # No sigdie handler
# do stuff that might die
}
# catch
HandleEvalError( $@ ) if $@;
}
</code>
<P><B>Pitfall 2:</B> The sigdie handler can be set at any time, including during compilation (via a BEGIN block).
If you set the handler during a BEGIN block and complitation fails before compiling your subroutine, you not only
get a function not found error, but you lose the information about the compilation error.
<P>Solution 1: Declare your handler subroutine before setting the handler. <BR>
<table><tr><td width=15%> </td><td>
<Note>It occurs to me that you might not have realized that you can do this.
Here is an example of to what I am refering. The solution is to move the subroutine
up in your code, before the BEGIN block:
<code>
BEGIN{ $SIG{__DIE__} = \&FatalErr }
# Real problem if compile fails before getting here.
sub FatalErr { # do stuff, maybe print @_ or something.
}
</code> Why does moving the sub up work? Well... BEGIN is actually just
another sub routine, except that it gets called as soon as its compiled.
INIT, by the way, is also a special sub routine, except that it gets called
as the last step of the compilation phase. </Note></td></tr></table>
Solution 2: Don't set your handler in a BEGIN block like that. If you want the
handle available after compilation is complete, but before code runs, then use an INIT block.
<P><B>Pitfall 3:</B> Not so much a pitfall as it is a reminder: Functions called with an ampersand
and no argument list are passed the current values of @_. This means (as [Plaid] once pointed out to me)
that your sigdie handler gets the same argument list that [die] got. One thing to note is that the file/line number
appending has already taken place by the time your handler gets @_.
<P><B>Pitfall 4:</B> When your handler is finished, Perl continues dieing. This means that not only does the
execution end, but Perl prints the error message to STDERR.
<P>
Solution: You might not have thought of that as a pitfall, but I certainly do. Sometimes you want to include
a lot of information about the failure. Maybe write it to a file or something, and then make the error message
the user sees be something like, "An error occurred. See log, '$log'\n". The trick here is
to keep in mind that Perl is about to [exit] anyway, so why not accelerate the process? BTW Don't do this in
your signal handler unless you are also checking <A HREF="index.pl?node_id=403#item__S">$^S</A>. People get mad
when [eval] blocks don't return.
<P><B>Pitfall 5:</B> Your handler only gets called if you use [die]. This means that if you aren't checking your
system calls and using [die] like C's Assert, then you won't get any added functionality.
<P>Solution: [die]. Everywhere.
<P>I hope this helps you out.
Please do read the documentation sited above.
Also, don't forget the [id://35320|power] of an END block.
<p><small>
A few of my past posts on the subject:<BR>
[id://21573]
[id://39345]
[id://37704]
</small>
42376
42376