Installing a Signal Handler (Perl Cookbook, 2nd Edition)

## 16.15. Installing a Signal Handler

### 16.15.1. Problem

You want to control how your program responds to signals. You need to do this if you want to catch Ctrl-C, avoid accumulating finished subprocesses, or prevent your process from dying when it writes to a child that has gone away.

### 16.15.2. Solution

Use the `%SIG` hash to install your own handler by name or by code reference:

>     $SIG{QUIT} = \&got_sig_quit;     # call &got_sig_quit for every SIGQUIT
>     $SIG{PIPE} = 'got_sig_pipe';     # call main::got_sig_pipe for every SIGPIPE
>     $SIG{INT}  = sub { $ouch++ };    # increment $ouch for every SIGINT

`%SIG` also lets you ignore a signal:

>     $SIG{INT} = 'IGNORE';            # ignore the signal INT

It also restores handling for that signal to the default:

>     $SIG{STOP} = 'DEFAULT';          # restore default STOP signal handling

### 16.15.3. Discussion

Perl uses the `%SIG` hash to control what happens when signals are received. Each key in `%SIG` corresponds to a signal. Each value is the action to take when Perl receives the corresponding signal. Perl provides two special behaviors: "`IGNORE`" to take no action when a particular signal is received, and "`DEFAULT`" to perform the default Unix action for that signal.

Although a C programmer might think of a signal as `SIGINT`, Perl uses just `INT`. Perl figures you only use signal names in functions that deal with signals, so the `SIG` prefix is redundant. This means that you'll assign to `$SIG{CHLD}` to change what your process does when it gets a `SIGCHLD`.

If you want to run your own code when a given signal is received, you have two choices of what to put in the hash: either a code reference or a subroutine name. (This means you can't name a signal handler IGNORE or DEFAULT if you store the string, but they'd be mighty strange names for signal handlers anyway.) If you use a subroutine name that isn't qualified by a package, Perl will interpret this name to be a function in the `main:`: package, not one in the package in which the handler was installed. A code reference refers to a subroutine in a particular package, so it is a better choice.

Perl calls your handler code with a single argument: the name of the signal that triggered it, such as "`INT`" or "`USR1`". Returning from a signal handler takes you back to whatever you were doing when the signal hit.

Perl defines two special signals, `_ _DIE_ _` and `_ _WARN_ _`, whose handlers are called whenever a Perl program emits warnings through `warn` or dies through `die`. This lets you catch such warnings, and selectively trap or propagate them. The `die` and `warn` handlers are disabled while they run, so you can safely `die` from a `_ _DIE_ _` handler or `warn` from a `_ _WARN_ _` handler without fear of recursion.

### 16.15.4. See Also

The "Signals" sections in Chapter 16 of Programming Perl and in perlipc(1); your system's sigaction(2), signal(3), and kill(2) manpages (if you have them)

Copyright © 2003 O'Reilly & Associates. All rights reserved.