如果我告诉 Perl 显式忽略一个信号,则 SIGINT 无效:
$SIG{INT} = 'IGNORE';
my $count = 0;
say $count++ and sleep 1 while 1;
然后按 Control-C,很明显,没有任何效果。另一方面,如果我告诉它什么都不做:
$SIG{INT} = sub { };
my $count = 0;
say $count++ and sleep 1 while 1;
然后按Control-C就有效果了!它从 sleep() 调用中唤醒程序并立即增加计数。忽略信号和告诉它什么都不做有什么区别?
在我的程序中,我想让代码在 SIGINT 上运行,而不会破坏任何东西。我想要类似的东西:
$SIG{INT} = sub { say "Caught SIGINT!"; return IGNORED; }; # runs without disrupting sleep