This works as expected since Perl 5.10.1: SIGINTs are trapped.
#!/usr/bin/perl
use strict;
use warnings;
$SIG{INT} = sub { die "Caught a sigint $!" };
sleep(20);
But here SIGINTs are not trapped.
#!/usr/bin/perl
use strict;
use warnings;
$SIG{INT} = sub { die "Caught a sigint $!" };
END {
sleep(20);
}
This can be fixed by setting the handler again in the END
block, like so:
END {
$SIG{INT} = sub { die "Caught a sigint $!" };
sleep(20);
}
But that won't work if you have more than one block: handlers must be set again for every block.
I've tried to figure this out and I can't find an explanation at perldoc. The only mention of this behaviour I can find is a footnote from Practical Perl Programming A D Marshall 1999-2005
Note Signals that are sent to your script can bypass the END blocks.
Would someone explain this?