Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: How do I pause real time in Perl

by kcott (Archbishop)
on Nov 02, 2013 at 08:06 UTC ( [id://1060907]=note: print w/replies, xml ) Need Help??


in reply to How do I pause real time in Perl

This code does what you want (at least, as far as I can tell from your very hard-to-read posting):

#!/usr/bin/env perl use strict; use warnings; use Time::HiRes qw{usleep}; use Time::Piece; my ($countdown, $check) = @ARGV; $countdown = 60 unless defined $countdown; $check ||= 10; # Avoid "Illegal modulus zero" error my $countdown_remaining = Time::Piece->strptime(0 => '%S'); $countdown_remaining += $countdown; my $continue_re = qr{(?:^$|^[Yy])}; print_timestamp('Started'); { local $| = 1; do { print_countdown($countdown_remaining); my $seconds_remaining = $countdown_remaining->epoch; print "\n" and last unless $seconds_remaining; if (($countdown - $seconds_remaining) % $check == 0 && $countdown > $seconds_remaining ) { print_timestamp("\nPaused "); print "Continue? ([Y]/n): "; chomp(my $reply = <STDIN>); last unless $reply =~ $continue_re; print_timestamp('Resumed'); } usleep 1_000_000; } while $countdown_remaining--; } print_timestamp("Finished"); sub print_countdown { printf "\r%s" => shift->strftime('%H:%M:%S'); } sub print_timestamp { print shift, ' at: ', scalar(localtime), "\n"; }

By default, it runs with a 60 second timer and 10 second check:

$ pm_time_countdown.pl Started at: Sat Nov 2 18:02:36 2013 00:00:50 Paused at: Sat Nov 2 18:02:46 2013 Continue ([Y]/n): Resumed at: Sat Nov 2 18:02:50 2013 ... Paused at: Sat Nov 2 18:03:35 2013 Continue ([Y]/n): Resumed at: Sat Nov 2 18:03:40 2013 00:00:00 Finished at: Sat Nov 2 18:03:50 2013

You can pass any values on the command line:

$ pm_time_countdown.pl 3 2 Started at: Sat Nov 2 18:30:23 2013 00:00:01 Paused at: Sat Nov 2 18:30:25 2013 Continue? ([Y]/n): Resumed at: Sat Nov 2 18:30:26 2013 00:00:00 Finished at: Sat Nov 2 18:30:27 2013

Currently, the countdown needs to be less than a day (you could extend that):

$ pm_time_countdown.pl 86337 600 Started at: Sat Nov 2 17:44:22 2013 23:48:57 Paused at: Sat Nov 2 17:54:23 2013 Continue ([Y]/n): n Finished at: Sat Nov 2 18:02:00 2013

The two modules I've used are both builtins: you have them already; there's nothing to install from CPAN.

Time::HiRes provides high resolution time functions: usleep() is the only one I've used here. The builtin sleep() function is not very accurate: the doco suggests the best you could guarantee might be: 0s < sleep(1) < 2s.

I've used Time::Piece to handle the countdown value and generate the 'HH:MM:SS' formats. As you can see, this was a much easier way to code it than your multiple '($end_time - $time) ...' technique.

The rest of the code is pretty straightforward and shouldn't hold any surprises. It should run under Perl v5.8 or later versions.

-- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found