Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: PERL Countdown Timer

by ikegami (Pope)
on Nov 15, 2004 at 19:29 UTC ( #407922=note: print w/ replies, xml ) Need Help??


in reply to PERL Countdown Timer

(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:

use strict; use warnings; my $time_left = 1*60*60; # in secs. while ($countdown--) { printf("\r%02d:%02d:%02d", $time_left / (60*60), $time_left / ( 60) % 60, $time_left % 60, ); sleep(1); } print("\rKABOOM!!!!\n");


Comment on Re: PERL Countdown Timer
Select or Download Code
Re^2: PERL Countdown Timer
by zentara (Archbishop) on Nov 16, 2004 at 18:13 UTC
    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).
Re^2: PERL Countdown Timer
by Anonymous Monk on Feb 13, 2013 at 17:53 UTC
    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; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-10-20 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls