Created
July 26, 2011 05:15
-
-
Save kentaro/1106028 to your computer and use it in GitHub Desktop.
Alarm::setTimeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
package Alarm::setTimeout; | |
use strict; | |
use warnings; | |
my @handlers; | |
$SIG{ALRM} = sub { | |
$_->() for @handlers; | |
}; | |
sub setTimeout { | |
my ($sub, $time) = @_; | |
my $self = __PACKAGE__->new; | |
push @handlers, sub { | |
$sub->(); | |
$self->fired(1); | |
}; | |
alarm $time / 1000; | |
$self; | |
} | |
sub new { | |
my $class = shift; | |
bless { fired => 0 }, $class; | |
} | |
sub fired { | |
my ($self, $fired) = @_; | |
$self->{fired} = $fired if defined $fired; | |
$self->{fired}; | |
} | |
sub DESTROY { | |
my $self = shift; | |
while (!$self->fired) { } | |
} | |
package main; | |
my $timer = Alarm::setTimeout::setTimeout( | |
sub { | |
warn '1sec!', | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment