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.