PROGRAMMERS: Timers in perl

STUCI auto chess program for yahoo
Post Reply
User avatar
zappa_engine
Moderator
Moderator
Posts: 117
Joined: 09 Oct 2007 23:28
Contact:

PROGRAMMERS: Timers in perl

Post by zappa_engine »

Okay, I've been wanting to implement multiple timers in STUCI for some time now. I was thinking back on a past project I worked on that used timers extensively and remembered how it managed to do that. I realized I could use those timer concepts here in Perl.

If you ever tried to use a timer in Perl you know, there is only one timer allowed to be run at once. It is set up through an alarm system where you assign a function to the variable $SIG{ALRM} . After that, you can set up the system to call the alarm with the command: alarm($time_to_wait_before_calling_alarm);

EX: This will print Hello World!, about 30 seconds from the time the alarm command is run.

Code: Select all

$SIG{ALRM} = sub { print "Hello World!"; }
alarm 30;
So how do you assign multiple alarms without replacing the function assigned to $SIG{ALRM}? Here is how the timers worked in my old project.

1. You have create and manage an array of timers.
2. There are 3 pieces to put in each element of the array (the timer's identifier name, the time the timer should run, the function to run when it's time)
3. Adding a timer element to the array will set the $SIG{ALRM} to call the array timer manager.
4. It will also make sure $SIG{ALRM} is set to be called as soon as the closest timer is found in the array.
5. It will add the element (timer_name, time_length_of_alarm, function) to the array.
6. As soon as $SIG{ALRM} is called, the next soonest timer is set to be called.

That's it. So after building the framework for these timer codes, you could write something like this:

Code: Select all

set_timer("my_first_timer",10, "sub {print "Hello World!"}" );
set_timer("my_second_timer",25, "functionSecondTimer();" );
set_timer("timer_3", 15, "clear_timer('my_second_timer');" );

sub functionSecondTimer () { print "This will never run...\n"; }
In summary, this code sets up 3 timers. In 10 seconds, it will print Hello World. In 25 seconds, it will run functionSecondTimer which will print This will never run... But in the third timer which clears in 15 seconds, it will remove that timer from the list, so that functionSecondTimer will never run.

Final notes...
You can notice the functions are either set within the function, or it can be a call to a pre-existing function. Also you should notice the function is set inside a string. Why is this? This allows the programmer to dynamically build functions and then run the eval command on the string. This is a really powerful feature. How could this be used? I'm not sure right now but it's there if you would want to play with it.
http://www.youtube.com/watch?v=gNASSON_JMU
Concerning STUCI and why it's never really done... here's a metaphor: Instead of being a construction worker, I'd rather be an architect.
Losing too many games because of a slow PC? No problem, nUCI it!

Post Reply