Tibco::Rv::Timer - Tibco Timer event object
my ( $timer ) = $rv->createTimer( interval => 10, sub { print "Timer event happened!\n"; } ); $timer->interval( 5 ); $timer->onEvent( );
A Tibco::Rv::Timer fires an event after every specified interval. It is a subclass of Tibco::Rv::Event, so Event methods are available to Timers (documentation on Event methods are reproduced here for convenience).
Tibco::Rv::Timer
%args: queue => $queue, interval => $interval, callback => sub { ... }
Creates a Tibco::Rv::Timer. If not specified, queue defaults to the Default Queue, interval defaults to 1, and callback defaults to:
sub { print "Timer fired\n" }
Create a Timer to cause an event to fire after every $interval seconds. The $interval can specify fractions of a second. When $queue dispatches such an event, it triggers the given callback.
$interval
$queue
Returns the number of seconds in the interval between firings of the Timer's event.
Set the interval at which the next Timer event will be dispatched.
Returns the queue on which this Timer's events will be dispatched.
Returns the callback code reference.
Trigger an event directly. The event will be processed as if it was triggered via the event queue.
Cancels the Timer. Called automatically when $timer goes out of scope. Calling DESTROY more than once has no effect.
$timer
As an alternative to passing in a callback function to the constructor, there is another way to handle events. You can subclass Tibco::Rv::Timer and override the onEvent method, as follows:
package MyTimer; use base qw/ Tibco::Rv::Timer /; sub new { my ( $proto, %args ) = @_; my ( $self ) = $proto->SUPER::new( %args ); # your initialization code return $self; } sub onEvent { my ( $self ) = @_; # process event here # $self->queue, $self->interval are available } # any other implementation code for your class 1;
The Tibco::Rv::Event onEvent method simply calls the callback, so overriding onEvent allows you to process the event however you want, and you can just not use the callback.
Tibco::Rv::Event
The advantages of this method of handling events are: it is more object-oriented; you have access to the queue and interval via the $self accessor methods; and, you can have more elaborate processing of timed events without having to shove it all into one callback.
$self
You can use your subclassed Timer as follows:
use Tibco::Rv; use MyTimer; my ( $rv ) = new Tibco::Rv; my ( $myTimer ) = new MyTimer( interval => 4 ); $rv->start;
Paul Sturm <sturm@branewave.com>
To install Tibco::Rv, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tibco::Rv
CPAN shell
perl -MCPAN -e shell install Tibco::Rv
For more information on module installation, please visit the detailed CPAN module installation guide.