加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

超时perl中的用户输入

发布时间:2020-12-16 06:23:29 所属栏目:大数据 来源:网络整理
导读:我想提示用户输入,在一段时间后,如果没有响应,脚本必须退出.我有这个代码 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
我想提示用户输入,在一段时间后,如果没有响应,脚本必须退出.我有这个代码

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 ..so)”
任何人都可以帮助如何将此功能与超时一起嵌入.

谢谢

解决方法

# 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';
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读