JavaScript EditorFreeware JavaScript Editor     Perl Tutorials 



Main Page Previous Section Next Section

Recipe 16.22 Turning Signals into Fatal Errors

16.22.1 Problem

END blocks aren't run when your program dies from an uncaught signal. Your program gets such signals, and you'd like your END blocks to have a chance to clean up.

16.22.2 Solution

Use the sigtrap pragma:

use sigtrap qw(die untrapped normal-signals);

16.22.3 Discussion

Untrapped signals cause your program to die without running END blocks. Although you could manually install signal handlers that call die, this becomes tedious for a lot of signals:

$SIG{INT} = $SIG{HUP} = $SIG{PIPE} = $SIG{TERM} = sub { die };

The sigtrap pragma provides a convenient shorthand for installing such handlers:

use sigtrap qw(die untrapped normal-signals);

The die import tells sigtrap to call die (you can also import stack-trace to install handlers that trigger stack traces). The untrapped import tells sigtrap to install handlers only for signals that don't already have them, so if you handle SIGPIPE yourself, sigtrap won't replace your handler.

normal-signals is one of several imports that specify predefined lists of useful signals to trap. The signal lists are given in Table 16-2.

Table 16-2. Signal lists

List

Signals

normal-signals

HUP, INT, PIPE, TERM

error-signals

ABRT, BUS, EMT, FPE, ILL, QUIT, SEGV, SYS, TRAP

old-interface-signals

ABRT, BUS, EMT, FPE, ILL, PIPE, QUIT, SEGV, SYS, TERM, TRAP

You can combine signal lists:

use sigtrap qw(die untrapped normal-signals error-signals);

You can even combine different handler types in one import list. Here we use untrapped to specify only the normal signals for which there is not already a handler installed, then use any to revert to sigtrap's default behavior of installing handlers for all signals in the named list:

use sigtrap qw( 
    die           untrapped    normal-signals
    stack-trace   any          error-signals         
);

16.22.4 See Also

Recipe 12.7; the documentation for the standard module sigtrap; Recipe 16.15

    Main Page Previous Section Next Section
    


    JavaScript EditorJavaScript Verifier     Perl Tutorials


    ©