We help IT Professionals succeed at work.

Perl Timer

TonyJix
TonyJix asked
on
9,076 Views
Last Modified: 2008-02-13
Hi,

I want to set a timer that will simply keep on doing something with a few seconds in between.
I tried the following, but it simply exists while the perl program should keep "printing time" every 2 seconds.

use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
$SIG{VTALRM} = sub { print time, "\n" };
setitimer(ITIMER_VIRTUAL, 10, 2.5);

Greetings.
Comment
Watch Question

Author

Commented:
"exists" should be "exits"
you have to initialise $SIG in your called function again

Author

Commented:
I don't understand
$SIG{VTALRM} = sub { print time, "\n"; $SIG{VTALRM} = sub { print time, "\n" }; };

Author

Commented:
Doesn't do anything. It doesn't even call the sub once.
It exists immediately.

I don't to use another solution.
All I want is perl to execute some code, then when it's finished it should wait a few seconds, then execute the same code again, and again, and again.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
HAHA
# if the work takes significant portion of the cycle, you can calculate required sleep duration as following:
my $cycle = 10;  # in seconds
while ( ) {
  my $time1 = time();  
  do_work();
  my $time2 = time();
  sleep( $cycle - ($time2 - $time1) );
}
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.