On Fri, Aug 20, 2010 at 04:34, Kammen van, Marco, Springer SBM NL
<[email protected]> wrote:
> Hi All,
>
> I want to use timers to check if certain variables are set and if not
> send some data back to a client...
>
> Been searching for this a while now, but all I can find on alarm are
> examples on timing out commands....
snip
All the [alarm][0] function does is send the [ALRM][0] signal to the
current process after X seconds. It is often used to turn a blocking
function into a non-blocking function (i.e. a timeout), but any code
can be put into the signal handler. Here is some code that does
something different with it:
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes qw/gettimeofday/;
sub increment_speed {
my $wait = shift;
my $count = 1;
my $continue = 1;
local $SIG{ALRM} = sub { $continue = 0 };
#run this loop for roughly $wait seconds
alarm $wait;
while ($continue) {
$count++;
}
return $count;
}
my $start = gettimeofday;
my $count = increment_speed(5);
my $end = gettimeofday;
my $elapsed = $end - $start;
my $average = $count/$elapsed;
print "$average ($count/$elapsed) average increments per second\n";
[0]: http://perldoc.perl.org/functions/alarm.html
[1]: http://en.wikipedia.org/wiki/SIGALRM
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/