Signals
Perl supports many facilities for Interprocess Communication (IPC), including signals and sockets. Regardless of which IPC form you use with Perl, you’ll almost always tangle with signals.
Signals can be generated from any source, including key sequences on your keyboard, an event on your system, or from a failed program.
For quite some time, Perl has used a simple signal handling
module, in which %SIG was populated with keys that represented the signal
handlers your system supported. You’d invoke one of these handlers
when Perl came across $SIG{'whatever_signal_here'}. In fact, even
in Perl 5.8, the %SIG interface is
the same, but the behavior of signals is entirely different. More on
this shortly.
For signals to be useful, they must be trapped and dealt with.
However, you shouldn’t try to do too much when you trap a signal.
Generally, you should only generate a warning and deal with the
condition that caused the error. In a way, Perl’s %SIG keeps you from doing too much. %SIG works like this:
$SIG{I_GOT_THIS_SIGNAL} = \&now_call_this_sub_to_handle_it;
sub now_call_this_sub_to_handle it {
...
}If you need to know which signals Perl is aware of, do a
'kill -l' on a Unix system, or use
Config.pm:
#!/usr/local/bin/perl -w
use Config;
print "Perl knows about the following signals:\n";
print $Config{sig_name_init}, "\n";Here’s a simple snippet that lets you know if someone sent you a
HUP signal:
$SIG{HUP} = \&hic_hup; sub hic_hup { my $signal = shift; # Don't die for now. Just ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access