Blob


1 #!/usr/bin/perl
3 # (Unix only) Write an infinite loop program that catches signals
4 # and reports which signal it caught and how many times it has seen
5 # that signal before. Exit if you catch the INT signal. If you can
6 # use the command-line kill, you can send signals like so:
7 #
8 # $ kill -USR1 12345
9 #
10 # If you can't use the command-line kill, write another program to
11 # send signals to it. You might be able to get away with a Perl
12 # one-liner:
13 #
14 # $ perl -e 'kill HUP = 12345'>
16 use v5.24;
17 use warnings;
18 use strict;
19 use utf8;
21 foreach (qw (int hup usr1 usr2)) {
22 my $sig = uc $_;
23 $SIG{$sig} = sub {
24 my $signal = $sig;
25 state $n;
26 printf("%s: %d\n", $signal, ++$n);
27 if ($signal eq 'INT') {
28 die;
29 }
30 };
31 }
32 while (1) {
34 }