Debian Codegraph @

modules/scripting/perl/lib/Atheme/Internal/Hooklist.pm

raw
1 package Atheme::Internal::Hooklist;
2
3 use strict;
4 use warnings;
5
6 use Atheme::Hooks;
7
8 use Carp;
9
10 sub new {
11 my ($class, $hookname) = @_;
12 $class = ref $class || $class;
13
14 croak "Tried to construct a hook list without a name" unless $hookname;
15
16 return bless { name => $hookname, hooks => [] }, $class;
17 }
18
19 sub add_hook {
20 my ($self, $hook) = @_;
21
22 my ($caller) = caller;
23 $Atheme::Hooks::hooks_by_package{$caller} ||= [];
24 push @{$Atheme::Hooks::hooks_by_package{$caller}}, { list => $self, hook => $hook };
25
26 if (scalar @{$self->{hooks}} == 0) {
27 enable_perl_hook_handler($self->{name})
28 }
29
30 push @{$self->{hooks}}, $hook;
31 }
32
33 sub del_hook {
34 my ($self, $hook) = @_;
35
36 my @newhooks;
37 foreach my $h (@{$self->{hooks}}) {
38 push @newhooks, $h unless $h == $hook;
39 }
40
41 if (scalar @newhooks == 0) {
42 disable_perl_hook_handler($self->{name});
43 }
44
45 $self->{hooks} = \@newhooks;
46 }
47
48 sub call_hooks {
49 my ($self, $arg) = @_;
50
51 foreach my $hook (@{$self->{hooks}}) {
52 $hook->($arg);
53 }
54 }
55
56 1;