Reputation: 119
I am brand new to perl and trying to add functionality to my scripts to catch both keyboard interrupts and term signals if thrown, then issue a OS command to handle. Also need to issue the same OS command and sub in the case that no signals are thrown and the script executed successfully (note the END block). As far as i can tell from research and from testing, you have to register a signal handler for each signal. In each case, they need to trigger the same sub. Is this possible to do in one $SIG, or do i have to register both, one for each signal? Here is my code.
sub issueKdestroy{
system("kdestroy -q");
exit(1);
}
$SIG{INT} = (\&issueKdestroy);
$SIG{TERM} = (\&issueKdestroy);
END{
issueKdestroy();
}
from testing, this does not work:
$SIG{'INT', 'TERM'} = (\&issueKdestroy);
It will not recognize an INT or TERM in the above code. suggestions?
Upvotes: 3
Views: 347
Reputation: 98498
$SIG{'INT', 'TERM'}
isn't what you mean; to assign values for both keys like that, use a hash slice (and provide two values):
@SIG{'INT', 'TERM'} = (\&issueKdestroy, \&issueKdestroy);
With a $
instead, you are invoking the perl4 style multidimensional array emulation that allowed multiple keys to be specified but stored them all in a single hash, equivalent to:
$SIG{ join $;, 'INT', 'TERM' }
where $;
is chr(0x1c)
by default.
Upvotes: 2