Specify a Perl signal handler using a number, not a name

I want to specify a signal handler in Perl, but using a number, not a name. Is this possible succinctly? The lack of symmetry with slaughter sticks out especially. For example, instead of

$SIG{USR2} = \&myhandler;

I would like to say

$SIG{12} = \&myhandler;

The best thing I have at the moment is to "use Config" and navigate $ Config {sig_name} based on the code per peroc perlipc. It is verbose and seems overly complex.

Rationale: I have wanted this on two occasions lately.

1: I get started by a buggy-parent process that incorrectly sets the signals that concern me, and I just want to reset everything to default. e.g. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=679630 The goal would be simple and brute force:

foreach my $i (1..32) { $SIG{$i} = 'DEFAULT'; }

2: , , script. , , , . , , , , . - - :

$ret = system("./other-program");
$SIG{$ret & 127} = 'DEFAULT';
kill $ret & 127, $$;
+5
2

:

use Config qw( %Config );

my @sig_name_by_num;
@sig_name_by_num[ split(' ', $Config{sig_num}) ] = split(' ', $Config{sig_name});

$SIG{$sig_name_by_num[12]} = \&handler;

:

use Config qw( %Config );

$SIG{$_} = 'DEFAULT' for split ' ', $Config{sig_name};

- -

$SIG{$_} = 'DEFAULT' for keys %SIG;

- -

$_ = 'DEFAULT' for values %SIG;

use Config qw( %Config );

my @sig_name_by_num;
@sig_name_by_num[ split(' ', $Config{sig_num}) ] = split(' ', $Config{sig_name});

my $sig_num = $? & 0x7F;
$SIG{$sig_name_by_num[$sig_num]} = 'DEFAULT';
kill($sig_num => $$);
+2

, :

$SIG{$_} = 'DEFAULT' for keys %SIG;

$Config{sig_name}/$Config{sig_num} - , , Unix-y, -

$sig_name = qx(kill -l $sig_num);

($sig_name , -

chomp($sig_name = qx(kill -l $sig_num));
($sig_name) = qx(kill -l $sig_num) =~ /(\S+)/;

)

, %Config, .

sub sig_no { chomp(my ($sig_no = qx(kill -l $_[0])); $sig_no }

$SIG{ sig_no($ret & 127) } = 'DEFAULT';
...
+2

All Articles