Signals in Perl, 5.10.1 vs 5.14.2

A Perl script that I've been working on wasn't working properly with Perl 5.14.2.

It turned out that the problem was really with my use of local signal handlers.

The following code works with 5.10.1 but not with 5.14.2:

use strict;
use warnings;
print "starting pid=$$n";
my $I = 0;
  my $pid = 0;
  sub handler {
    my $sig = shift;
    $I += 1;
    print "caught $sign";
    local $SIG{$sig} = 'IGNORE'; # doesn't work with local!!
    kill($sig, -$$);
    print "end of handlern";
  local $SIG{INT} = &handler;
  kill('INT', $$);
print "ending I=$In";

With 5.10.1, it calls handler() twice and then exits with I=2; with 5.14.2 it calls the handler many times before crashing with a segfault.

Removing the local from the inner $SIG{$sig} allows it to work with 5.14.2, which makes sense if I'm right in thinking that that command changes the immediately-containing signal handler, and not the global one.

I found the Perl documentation a bit vague on this topic, so this is here in case it's useful to someone else.

Leave a Reply

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