in reply to Re: Waiting for Alarm
in thread Waiting for Alarm

When I tried using sleep in the loop, it bombed out after the first alarm -- I got one print from the handler, and then the program exited. However, I just tried again with this:

#!/usr/bin/perl

use Time::HiRes qw ( setitimer ITIMER_REAL time );
my $count;

$SIG{ALRM} = sub { print time, "\n" };
setitimer(ITIMER_REAL, 1, 1);

while (1) {
        sleep 1;
        }

and it worked fine. If this is reliable, it does the job for me. But I wonder why the first time it only worked for the first iteration...

Thanks!

Replies are listed 'Best First'.
Re^3: Waiting for Alarm
by n8ur (Acolyte) on Feb 16, 2008 at 18:23 UTC
    Ignore the "my $count" -- that's left over from an earlier experiment. Apart from the while loop, this is based on an example in the Time::HiRes documentation.