1Class::Trigger(3)     User Contributed Perl Documentation    Class::Trigger(3)
2
3
4

NAME

6       Class::Trigger - Mixin to add / call inheritable triggers
7

SYNOPSIS

9         package Foo;
10         use Class::Trigger;
11
12         sub foo {
13             my $self = shift;
14             $self->call_trigger('before_foo');
15             # some code ...
16             $self->call_trigger('middle_of_foo');
17             # some code ...
18             $self->call_trigger('after_foo');
19         }
20
21         package main;
22         Foo->add_trigger(before_foo => \&sub1);
23         Foo->add_trigger(after_foo => \&sub2);
24
25         my $foo = Foo->new;
26         $foo->foo;                    # then sub1, sub2 called
27
28         # triggers are inheritable
29         package Bar;
30         use base qw(Foo);
31
32         Bar->add_trigger(before_foo => \&sub);
33
34         # triggers can be object based
35         $foo->add_trigger(after_foo => \&sub3);
36         $foo->foo;                    # sub3 would appply only to this object
37

DESCRIPTION

39       Class::Trigger is a mixin class to add / call triggers (or hooks) that
40       get called at some points you specify.
41

METHODS

43       By using this module, your class is capable of following two methods.
44
45       add_trigger
46             Foo->add_trigger($triggerpoint => $sub);
47             $foo->add_trigger($triggerpoint => $sub);
48
49           Adds triggers for trigger point. You can have any number of trig‐
50           gers for each point. Each coderef will be passed a the object ref‐
51           erence, and return values will be ignored.
52
53           If "add_trigger" is called as object method, whole current trigger
54           table will be copied onto the object and the new trigger added to
55           that. (The object must be implemented as hash.)
56
57             my $foo = Foo->new;
58
59             # this trigger ($sub_foo) would apply only to $foo object
60             $foo->add_trigger($triggerpoint => $sub_foo);
61             $foo->foo;
62
63             # And not to another $bar object
64             my $bar = Foo->new;
65             $bar->foo;
66
67           Any triggers added to the class after adding a trigger to an object
68           will not be fired for the object because the object now has a pri‐
69           vate copy of the triggers.
70
71       call_trigger
72             $foo->call_trigger($triggerpoint, @args);
73
74           Calls triggers for trigger point, which were added via "add_trig‐
75           ger" method. Each triggers will be passed a copy of the object as
76           the first argument.  Remaining arguments passed to "call_trigger"
77           will be passed on to each trigger.  Triggers are invoked in the
78           same order they were defined.
79

TRIGGER POINTS

81       By default you can make any number of trigger points, but if you want
82       to declare names of trigger points explicitly, you can do it via
83       "import".
84
85         package Foo;
86         use Class::Trigger qw(foo bar baz);
87
88         package main;
89         Foo->add_trigger(foo  => \&sub1); # okay
90         Foo->add_trigger(hoge => \&sub2); # exception
91

FAQ

93       Acknowledgement: Thanks to everyone at POOP mailing-list
94       (http://poop.sourceforge.net/).
95
96       Q.  This module lets me add subs to be run before/after a specific sub‐
97           routine is run.  Yes?
98
99       A.  You put various call_trigger() method in your class.  Then your
100           class users can call add_trigger() method to add subs to be run in
101           points just you specify (exactly where you put call_trigger()).
102
103       Q.  Are you aware of the perl-aspects project and the Aspect module?
104           Very similar to Class::Trigger by the look of it, but its not
105           nearly as explicit.  Its not necessary for foo() to actually say
106           "triggers go *here*", you just add them.
107
108       A.  Yep ;)
109
110           But the difference with Aspect would be that Class::Trigger is so
111           simple that it's easy to learn, and doesn't require 5.6 or over.
112
113       Q.  How does this compare to Sub::Versive, or Hook::LexWrap?
114
115       A.  Very similar. But the difference with Class::Trigger would be the
116           explicitness of trigger points.
117
118           In addition, you can put hooks in any point, rather than pre or
119           post of a method.
120
121       Q.  It looks interesting, but I just can't think of a practical example
122           of its use...
123
124       A.  (by Tony Bowden)
125
126           I originally added code like this to Class::DBI to cope with one
127           particular case: auto-upkeep of full-text search indices.
128
129           So I added functionality in Class::DBI to be able to trigger an
130           arbitary subroutine every time something happened - then it was a
131           simple matter of setting up triggers on INSERT and UPDATE to rein‐
132           dex that row, and on DELETE to remove that index row.
133
134           See Class::DBI::mysql::FullTextSearch and its source code to see it
135           in action.
136

AUTHOR

138       Original idea by Tony Bowden <tony@kasei.com> in Class::DBI.
139
140       Code by Tatsuhiko Miyagawa <miyagawa@bulknews.net>.
141
142       This library is free software; you can redistribute it and/or modify it
143       under the same terms as Perl itself.
144

SEE ALSO

146       Class::Data::Inheritable
147
148
149
150perl v5.8.8                       2005-08-23                 Class::Trigger(3)
Impressum