Recipe 16.16 Temporarily Overriding a Signal Handler

16.16.1 Problem

You want to install a signal handler only for a particular subroutine. For instance, your subroutine catches SIGINT, and you don't want to disturb SIGINT handling outside the subroutine.

16.16.2 Solution

Use local to temporarily override a signal's behavior:

# the signal handler
sub ding {
    $SIG{INT} = \&ding;
    warn "\aEnter your name!\n";

# prompt for name, overriding SIGINT
sub get_name {
    local $SIG{INT} = \&ding;
    my $name;

    print "Kindly Stranger, please enter your name: ";
    chomp( $name = <> );
    return $name;

16.16.3 Discussion

You must use local rather than my to save away one value out of %SIG. The change remains in effect throughout the execution of that block, including in anything called from it. In this case, that's the get_name subroutine. If the signal is delivered while another function that your function calls is running, your signal handler is triggeredunless the called subroutine installs its own signal handler. The previous value of the hash is automatically restored when the block exits. This is one of the (few) places where dynamic scoping is more convenient than confusing.

16.16.4 See Also

Recipe 10.13; Recipe 16.15; Recipe 16.18