release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change
on this file since b06a888 was
ee183be,
checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
|
Break perlwrap.pm into multiple files.
Break perlwrap.pm out into .pm files in perl/lib. With this commit, we
keep perlwrap.pm around and still load it as before. The next commit
will delete perlwrap.pm and start loading perl files directly from
disk at runtime.
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[ee183be] | 1 | use strict; |
---|
| 2 | use warnings; |
---|
| 3 | |
---|
| 4 | package BarnOwl::Hook; |
---|
| 5 | |
---|
| 6 | =head1 BarnOwl::Hook |
---|
| 7 | |
---|
| 8 | =head1 DESCRIPTION |
---|
| 9 | |
---|
| 10 | A C<BarnOwl::Hook> represents a list of functions to be triggered on |
---|
| 11 | some event. C<BarnOwl> exports a default set of these (see |
---|
| 12 | C<BarnOwl::Hooks>), but can also be created and used by module code. |
---|
| 13 | |
---|
| 14 | =head2 new |
---|
| 15 | |
---|
| 16 | Creates a new Hook object |
---|
| 17 | |
---|
| 18 | =cut |
---|
| 19 | |
---|
| 20 | sub new { |
---|
| 21 | my $class = shift; |
---|
| 22 | return bless [], $class; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | =head2 run [ARGS] |
---|
| 26 | |
---|
| 27 | Calls each of the functions registered with this hook with the given |
---|
| 28 | arguments. |
---|
| 29 | |
---|
| 30 | =cut |
---|
| 31 | |
---|
| 32 | sub run { |
---|
| 33 | my $self = shift; |
---|
| 34 | my @args = @_; |
---|
| 35 | return map {$self->_run($_,@args)} @$self; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | sub _run { |
---|
| 39 | my $self = shift; |
---|
| 40 | my $fn = shift; |
---|
| 41 | my @args = @_; |
---|
| 42 | no strict 'refs'; |
---|
| 43 | return $fn->(@args); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | =head2 add SUBREF |
---|
| 47 | |
---|
| 48 | Registers a given subroutine with this hook |
---|
| 49 | |
---|
| 50 | =cut |
---|
| 51 | |
---|
| 52 | sub add { |
---|
| 53 | my $self = shift; |
---|
| 54 | my $func = shift; |
---|
| 55 | die("Not a coderef!") unless ref($func) eq 'CODE' || !ref($func); |
---|
| 56 | return if grep {$_ eq $func} @$self; |
---|
| 57 | push @$self, $func; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | =head2 clear |
---|
| 61 | |
---|
| 62 | Remove all functions registered with this hook. |
---|
| 63 | |
---|
| 64 | =cut |
---|
| 65 | |
---|
| 66 | sub clear { |
---|
| 67 | my $self = shift; |
---|
| 68 | @$self = (); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | 1; |
---|
Note: See
TracBrowser
for help on using the repository browser.