Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

CountDownTimer for my perl program,

by venky4289 (Novice)
on Feb 22, 2013 at 06:17 UTC ( [id://1020073]=perlquestion: print w/replies, xml ) Need Help??

venky4289 has asked for the wisdom of the Perl Monks concerning the following question:

Hi I need to add countdown timer to my perl program which starts from 00:25:00(25 minutes)when i start running the script and the program should abort/exit when the timer becomes 00:00:00 . thanks in advance.

Replies are listed 'Best First'.
Re: CountDownTimer for my perl program,
by tmharish (Friar) on Feb 22, 2013 at 06:23 UTC
Re: CountDownTimer for my perl program,
by vinoth.ree (Monsignor) on Feb 22, 2013 at 07:21 UTC

    tmharish ++

    Here is the exact code to timeout for 25 minutes.

    use strict; use warnings; use utf8; eval { local %SIG; $SIG{ALRM}= sub{ die "timeout reached, after 25 minutes!\n"; }; alarm 25*60; # 25 minutes. while(1) { sleep(1); print "Hello Perl\n"; } alarm 0; }; alarm 0; if($@) { print "Error: $@\n"; } exit(0);

      See the warning about using sleep and alarm being used in tandem.

      --MidLifeXis

Re: CountDownTimer for my program
by ww (Archbishop) on Feb 22, 2013 at 13:34 UTC
    Since you already have answers to your lazy (homework? job requirement?) question, TIMTOWTDI (but you'll need to adapt, almost trivially, to make this abort some other running program... by, for example, killing the other's PID or similar):
    #! /usr/bin/perl use 5.016; # 1020073 # Timeout Timer w/o eval or SIG print "How many minutes? "; my $min = <>; chomp $min; my $time_remaining = ($min * 60); sub countdown { my $t_left = shift; while ($t_left > 0) { sleep 1; --$t_left; say "Seconds remaining: $t_left"; #elaborate this sub if you want + output in min:sec format } } say countdown($time_remaining); print "\a" x 5 . "\t\t $time_remaining countdown is finished\n";

    But see also On asking for help & How do I post a question effectively? ... and start showing some effort!


    If you didn't program your executable by toggling in binary, it wasn't really programming!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-23 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found