Perl Signal Handling

Signal handling in Perl is one of the latest in many things I have had to learn recently. When I started looking for information on this, I of course began by googling for it first, and then resorting the Other Bible before going down some path. However in this case I found many examples where they simply assigned a signal handler for each signal they want to change from their default behavior:

sub INT_handler {
   print("Don't Interrupt!n");
}
 
$SIG{'INT'} = 'INT_handler';

I found this to be quite cumbersome, especially if you are trying to handle multiple signals (in my case almost all possible signals because I need to cleanup lock file). After digging deeper, I came across sigtrap, which allows you to assign handling methods to an individual signal or groups of signals.
A Simple Example:

use sigtrap qw{handler sigHandler normal-signals error-signals};
 
sub sigHandler {
   my $sig = shift;
   $sig = defined $sig ? $sig : "????";
   print STDERR "Signal caught: SIG$sig";
}

I would highly recommend you read further into what each of the signal handler classes for sigtrap are (I.E. normal-signals, error-signals and etc). Hopefully if I have some time I’ll try to read through them and expand upon them further here…

This entry was posted in Software Development and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">