For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2006 > Event Timer in Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Event Timer in Perl
SkyBlueshoes

2006-05-20, 9:58 pm

I have a script that uses an infinite loop, I'm wanting to be able to
set a timer that when expired will run a subroutine, something sort of
like Poe's callback timer feature. How would I go about doing this? I
need to be able to set multiple timers on the fly within my script and
have them only run once.

Sky Blueshoes
Jeff Pang

2006-05-21, 3:57 am


>I have a script that uses an infinite loop, I'm wanting to be able to
>set a timer that when expired will run a subroutine, something sort of
>like Poe's callback timer feature. How would I go about doing this? I
>need to be able to set multiple timers on the fly within my script and
>have them only run once.
>

Do you want the alarm() calling?see 'perldoc -f alarm' pls.

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
Stephen Kratzer

2006-05-22, 7:00 pm

On Saturday 20 May 2006 23:13, SkyBlueshoes wrote:
> I have a script that uses an infinite loop, I'm wanting to be able to
> set a timer that when expired will run a subroutine, something sort of
> like Poe's callback timer feature. How would I go about doing this? I
> need to be able to set multiple timers on the fly within my script and
> have them only run once.
>
> Sky Blueshoes


I'm sure there's a better way, but here's a simple scipt using threads and a
simple timer subroutine:

#!/usr/bin/perl -w
use strict;
use threads;

my $timer1 = threads->create(\&timer, \&timer_test, 1, 0);
$timer1->join();

sub timer {
my($subroutine, $interval, $max_iteration) = @_;
for (my $count = 1; $max_iteration == 0 || $count <= $max_iteration;
$count++) {
sleep $interval;
&$subroutine;
}
}

sub timer_test {
print "Testing...\n"
}

You'd need to modify the timer subroutine in order to pass arguments to the
routine executed by it.

Stephen Kratzer
CTI Networks, Inc.
Zentara

2006-05-23, 7:58 am

On Mon, 22 May 2006 10:11:26 -0400, kratzers@pa.net (Stephen Kratzer)
wrote:

>On Saturday 20 May 2006 23:13, SkyBlueshoes wrote:
>
>I'm sure there's a better way, but here's a simple scipt using threads and a
>simple timer subroutine:
>
>#!/usr/bin/perl -w
>use strict;
>use threads;
>
>my $timer1 = threads->create(\&timer, \&timer_test, 1, 0);
>$timer1->join();
>
>sub timer {
> my($subroutine, $interval, $max_iteration) = @_;
> for (my $count = 1; $max_iteration == 0 || $count <= $max_iteration;
>$count++) {
> sleep $interval;
> &$subroutine;
> }
>}
>
>sub timer_test {
> print "Testing...\n"
>}
>
>You'd need to modify the timer subroutine in order to pass arguments to the
>routine executed by it.
>
>Stephen Kratzer
>CTI Networks, Inc.


there are also the Glib event loop which is
used by Gtk2, it works in the console too.
See:
http://perlmonks.org?node_id=538341


There are also a couple of "event libraries", go to
http://cpan.org and search for "event".




--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com