如何在 Perl 中实时暂停 [英] How to pause real time in Perl

查看:59
本文介绍了如何在 Perl 中实时暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Perl 很陌生,这是我在这里的第一篇文章,所以请保持温和.

I am quite new to Perl and this is my first post here so be gentle.

我在 60 秒的倒数计时器中使用实时,并且需要能够每 10 秒停止一次,然后用户必须输入他们是否愿意在每 10 秒的间隔继续倒计时.计时器有效,我只是不知道如何暂停它以便用户输入他们的回复.尽可能基本的答案将非常受欢迎,因为我还不太了解.谢谢,这是我目前的代码.

I am using real time in a countdown timer of 60 seconds and need to be able to stop it every 10 seconds and the user must then input whether they would like to continue the countdown or not at each interval of 10. The timer works, I just can't figure out how to pause it for the user to input their response. As basic of an answer as possible would be very welcomed as I do not understand a lot yet. Thank you and here is my code so far.

my $countdown = 1*60; #60 seconds
$| = 1; #disable output buffering

my $start_time = time;
my $end_time = $start_time + $countdown;


for (;;) {
    my $time = time;
    last if ($time >= $end_time);
    printf("\r%02d:%02d:%02d",
        ($end_time - $time) / (1*60),
        ($end_time - $time) / 60%60,
        ($end_time - $time) % 60,
    );

    sleep(1);
}

推荐答案

在收到用户响应后更新 $end_time.

Update $end_time after getting the response from the user.

while (1) {
    $countdown--;
    $time = time;
    last if ($time >= $end_time);
    printf("\r%02d:%02d:%02d",
        ($end_time - $time) / (1*60),
        ($end_time - $time) / 60%60,
        ($end_time - $time) % 60,
    );

    sleep(1);
    if ($countdown%10 == 0) {
        print "Continue? ";
        $answer = <>;
        chomp $answer;
        last if $answer ne 'y';
        $end_time = time + $countdown;
    }
}

这篇关于如何在 Perl 中实时暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆