我想提示用户输入,如果一段时间后没有响应,则脚本必须退出。我有这个
的代码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 inputn";
$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 "$countn"; 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 inputn";
$answer = 'A';
}