我喜欢在后面跳while(1)
。怎么做?检查一个特殊变量last
是不行的,因为 while 表达式包含一个阻塞调用,所以如果检查表达式就太晚了。
#!/usr/bin/perl
use strict;
use warnings;
use feature qw( say );
use sigtrap 'handler', \&hup_handler, 'HUP';
my $counter = 0;
sub hup_handler { say 'HUP!!!'; $counter = 0; return; }
say 'It starts here';
while ( 1 ) {
sleep( 1 ); # Blocking call is in reality within while expression.
say ++$counter;
}
say 'It ends here';