source: perl/lib/BarnOwl/Timer.pm @ b120bd3

release-1.10release-1.7release-1.8release-1.9
Last change on this file since b120bd3 was 8d16e58, checked in by Nelson Elhage <nelhage@mit.edu>, 14 years ago
Allow explicitly cancelling timers from perl.
  • Property mode set to 100644
File size: 788 bytes
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Timer;
5
6sub new {
7    my $class = shift;
8    my $args = shift;
9
10    my $cb = $args->{cb};
11    die("Invalid callback pased to BarnOwl::Timer\n") unless ref($cb) eq 'CODE';
12
13    my $self = {cb => $cb};
14
15    bless($self, $class);
16
17    $self->{timer} = BarnOwl::Internal::add_timer($args->{after} || 0,
18                                                  $args->{interval} || 0,
19                                                  $self);
20    return $self;
21}
22
23sub stop {
24    my $self = shift;
25    if(defined($self->{timer})) {
26        BarnOwl::Internal::remove_timer($self->{timer});
27        undef $self->{timer};
28    }
29}
30
31sub do_callback {
32    my $self = shift;
33    $self->{cb}->($self);
34}
35
36sub DESTROY {
37    my $self = shift;
38    $self->stop;
39}
40
41
421;
Note: See TracBrowser for help on using the repository browser.