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;
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"; }
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.