Signal Name Number 2

I wondered if there was a module or pragma defining names for the signal numbers returned by the system call? For instance:

 use feature qw(say); use strict; use warnings; my $cmd = 'sleep 10'; my $res = system $cmd; my $signal = $res & 127; if ( $res == -1 ) { die "Failed to execute: $!\n"; } elsif ( $signal == 2 ) { say " Aborted by user."; } elsif ( $signal ) { printf " Command '%s' Died with signal %d, %s coredump.\n", $cmd, $signal, ( $? & 128 ) ? 'with' : 'without'; } 

Instead of $signal == 2 , I think it would be better to use something like $signal == SIG_ABRT to serve the code ..

+3
source share
2 answers

Yes, this is defined in the POSIX module:

 % perl -e 'use POSIX; printf "%s\n", SIGABRT;' 6 
+2
source

There are several ways to do this. You saw a method using POSIX , which I usually avoid, because by default it imports a huge number of characters (about six hundred) into the current package. Even if you restrict it to the names you need for this, by writing

 use POSIX ':signal_h'; 

there are still about fifty names left. It is also a trial and error issue for detecting a signal name with its number using this module.

You can also build a table of signals that your Perl has created using the Config module, which exports a list of signal names and their corresponding numbers to $Config{sig_name} and $Config{sig_num} .

 use strict; use warnings; use Config '%Config'; my @sig_names; @sig_names[ split ' ', $Config{sig_num} ] = split ' ', $Config{sig_name}; printf "Signal number 2 is %s\n", $sig_names[2]; 

Output

 Signal number 2 is INT 

Or you can use the IPC::Signal module, which conveniently transfers all this

 use strict; use warnings; use IPC::Signal 'sig_name'; printf "Signal number 2 is %s\n", sig_name(2); 

Output

 Signal number 2 is INT 
+6
source

Source: https://habr.com/ru/post/1239163/


All Articles