Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

PERL Countdown Timer

by vicpylon (Initiate)
on Nov 15, 2004 at 19:04 UTC ( #407906=perlquestion: print w/ replies, xml ) Need Help??
vicpylon has asked for the wisdom of the Perl Monks concerning the following question:

I am but a humble seeker of wisdom and respectfully ask the following. I have to come up with a timer that counts down from a user selected time (1-12 hours) and then produces a message (FINISHED). Problem is I can't seem to get my head around how to do it. Yes, I am a rookie, but this is giving me a headache. Any help is appreciated. Vic

Comment on PERL Countdown Timer
Replies are listed 'Best First'.
Re: PERL Countdown Timer
by ikegami (Pope) on Nov 15, 2004 at 19:29 UTC

    (A quick aside: The language is Perl, not PERL.)

    use strict; use warnings; my $countdown = 1*60*60; # in secs. $| = 1; my $beg_time = time; my $end_time = $beg_time + $countdown; for (;;) { my $time = time; last if ($time >= $end_time); printf("\r%02d:%02d:%02d", ($end_time - $time) / (60*60), ($end_time - $time) / ( 60) % 60, ($end_time - $time) % 60, ); sleep(1); } print("\rKABOOM!!!!\n");

    Below is an alternative, but it will drift. That means that if you count down for 12 hours, it may take 12 hours and 5 minutes, or even much more than that, depending on CPU utilisation of other processes and more. I'm providing it as an example of what you should NOT do.

    Bad code:

      A minor nit-pick, you need a
      $|++;
      in your code, or the timer will not update the display properly.

      I'm not really a human, but I play one on earth. flash japh
        oops, right, you need that on non-Windows systems. I tested my code in Windows, which line-flushes on \r and \n (as opposed to just on \n).
      I put it in a simple sub. Easy to adjust if you want to include additional parameters such as a pre or post message.
      countdown(10); sub countdown($) { #countdown(seconds); my ($duration) = @_; my $end_time = time + $duration; my $time = time; while ($time < $end_time) { $time = time; printf("\r%02d:%02d:%02d", ($end_time - $time) / (60*60), ($en +d_time - $time) / (60) % 60,($end_time - $time) % 60); #00:00:10 $|++; sleep 1; } }

      countdown(10, "before", "after"); sub countdown($$$) { #countdown(seconds, "premessage", "postmessage"); my ($duration, $premessage, $postmessage) = @_; my $end_time = time + $duration; my $time = time; while ($time < $end_time) { $time = time; printf("\r%s %02d:%02d:%02d %s", $premessage, ($end_time - $ti +me) / (60*60), ($end_time - $time) / (60) % 60,($end_time - $time) % +60, $postmessage); #before 00:00:10 after $|++; sleep 1; } }
Re: PERL Countdown Timer
by CountZero (Bishop) on Nov 15, 2004 at 19:28 UTC
    Three steps should do the trick:
    1. Calculate how many seconds the countdown should run and store this in $seconds.
    2. sleep $seconds;
    3. print "Finished"

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: PERL Countdown Timer
by TedPride (Priest) on Nov 16, 2004 at 15:39 UTC
    Here you go. I included some error checking in case someone mixed letters in with the number, or left out the numerical part entirely:
    use strict; use warnings; print "How long should I sleep, in hours? "; my $wait = <STDIN>; ($wait) = $wait =~ /(\d+)/; $wait -= 0; my $end = time() + $wait * 3600; sleep 1 while time() < $end; print "FINISHED";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://407906]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2015-07-30 05:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls