source: perl/lib/BarnOwl/Hook.pm @ 0e5afa2

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 0e5afa2 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
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Hook;
5
6=head1 BarnOwl::Hook
7
8=head1 DESCRIPTION
9
10A C<BarnOwl::Hook> represents a list of functions to be triggered on
11some event. C<BarnOwl> exports a default set of these (see
12C<BarnOwl::Hooks>), but can also be created and used by module code.
13
14=head2 new
15
16Creates a new Hook object
17
18=cut
19
20sub new {
21    my $class = shift;
22    return bless [], $class;
23}
24
25=head2 run [ARGS]
26
27Calls each of the functions registered with this hook with the given
28arguments.
29
30=cut
31
32sub run {
33    my $self = shift;
34    my @args = @_;
35    return map {$self->_run($_,@args)} @$self;
36}
37
38sub _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
48Registers a given subroutine with this hook
49
50=cut
51
52sub 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
62Remove all functions registered with this hook.
63
64=cut
65
66sub clear {
67    my $self = shift;
68    @$self = ();
69}
70
71
721;
Note: See TracBrowser for help on using the repository browser.