我想提示用户输入,一段时间后,如果没有响应,脚本必须退出。我有这个代码
eval {
local $SIG{ALRM} = sub { die "timeout getting the input \n" };
alarm 5;
$answer = <STDIN>;
alarm 0;
chomp $answer;
};
if ($@) {
#die $@ if $@ ne "timeout getting the input\n";
$answer = 'A';
}
警报超时按预期工作,但我希望在每秒递减类似于倒计时后有一个额外的打印语句(例如 10 秒说“10 ...9 ..8 ..等)”
任何人都可以帮助如何将此功能与超时一起嵌入。
谢谢
最佳答案
# disable output buffering
$| = 1;
my $answer;
eval {
my $count = 10;
local $SIG{ALRM} = sub {
# print counter and set alaram again
if (--$count) { print "$count\n"; alarm 1 }
# no more waiting
else { die "timeout getting the input \n" }
};
# alarm every second
alarm 1;
$answer = <STDIN>;
alarm 0;
chomp $answer;
};
if ($@) {
#die $@ if $@ ne "timeout getting the input\n";
$answer = 'A';
}
关于perl - 超时 perl 中的用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24010716/