• Home
  • History
  • Annotate
  • only in this directory
NameDateSize

..11-Apr-2013244

ChangesH A D20-Feb-20133 KiB

inc/H11-Apr-20136

lib/H05-Apr-20133

Makefile.PLH A D20-Feb-2013259

MANIFESTH A D20-Feb-2013721

META.ymlH A D20-Feb-2013626

READMEH A D20-Feb-20135.2 KiB

t/H11-Apr-201313

README

1NAME
2    Class::Trigger - Mixin to add / call inheritable triggers
3
4SYNOPSIS
5      package Foo;
6      use Class::Trigger;
7
8      sub foo {
9          my $self = shift;
10          $self->call_trigger('before_foo');
11          # some code ...
12          $self->call_trigger('middle_of_foo');
13          # some code ...
14          $self->call_trigger('after_foo');
15      }
16
17      package main;
18      Foo->add_trigger(before_foo => \&sub1);
19      Foo->add_trigger(after_foo => \&sub2);
20
21      my $foo = Foo->new;
22      $foo->foo;            # then sub1, sub2 called
23
24      # triggers are inheritable
25      package Bar;
26      use base qw(Foo);
27
28      Bar->add_trigger(before_foo => \&sub);
29
30      # triggers can be object based
31      $foo->add_trigger(after_foo => \&sub3);
32      $foo->foo;            # sub3 would appply only to this object
33
34DESCRIPTION
35    Class::Trigger is a mixin class to add / call triggers (or hooks) that
36    get called at some points you specify.
37
38METHODS
39    By using this module, your class is capable of following two methods.
40
41    add_trigger
42          Foo->add_trigger($triggerpoint => $sub);
43          $foo->add_trigger($triggerpoint => $sub);
44
45          Foo->add_trigger( name => $triggerpoint,
46                            callback => sub {return undef},
47                            abortable => 1); 
48
49          # no further triggers will be called. Undef will be returned.
50
51        Adds triggers for trigger point. You can have any number of triggers
52        for each point. Each coderef will be passed a reference to the
53        calling object, as well as arguments passed in via run_trigger.
54
55        If add_trigger is called with named_parameters and the "abortable"
56        parameter is passed a true value, a false return values will stop
57        processing of this trigger point. If the trigger is "abortable"
58        return the return value from the last callback processed will be
59        returned to calling code.
60
61        If "add_trigger" is called without the "abortable" flag, return
62        values will be ignored.
63
64        If "add_trigger" is called as object method, whole current trigger
65        table will be copied onto the object and the new trigger added to
66        that. (The object must be implemented as hash.)
67
68          my $foo = Foo->new;
69
70          # this trigger ($sub_foo) would apply only to $foo object
71          $foo->add_trigger($triggerpoint => $sub_foo);
72          $foo->foo;
73
74          # And not to another $bar object
75          my $bar = Foo->new;
76          $bar->foo;
77
78    call_trigger
79          $foo->call_trigger($triggerpoint, @args);
80
81        Calls triggers for trigger point, which were added via "add_trigger"
82        method. Each triggers will be passed a copy of the object as the
83        first argument. Remaining arguments passed to "call_trigger" will be
84        passed on to each trigger. Triggers are invoked in the same order
85        they were defined.
86
87TRIGGER POINTS
88    By default you can make any number of trigger points, but if you want to
89    declare names of trigger points explicitly, you can do it via "import".
90
91      package Foo;
92      use Class::Trigger qw(foo bar baz);
93
94      package main;
95      Foo->add_trigger(foo  => \&sub1); # okay
96      Foo->add_trigger(hoge => \&sub2); # exception
97
98FAQ
99    Acknowledgement: Thanks to everyone at POOP mailing-list
100    (http://poop.sourceforge.net/).
101
102    Q.  This module lets me add subs to be run before/after a specific
103        subroutine is run. Yes?
104
105    A.  You put various call_trigger() method in your class. Then your class
106        users can call add_trigger() method to add subs to be run in points
107        just you specify (exactly where you put call_trigger()).
108
109    Q.  Are you aware of the perl-aspects project and the Aspect module?
110        Very similar to Class::Trigger by the look of it, but its not nearly
111        as explicit. Its not necessary for foo() to actually say "triggers
112        go *here*", you just add them.
113
114    A.  Yep ;)
115
116        But the difference with Aspect would be that Class::Trigger is so
117        simple that it's easy to learn, and doesn't require 5.6 or over.
118
119    Q.  How does this compare to Sub::Versive, or Hook::LexWrap?
120
121    A.  Very similar. But the difference with Class::Trigger would be the
122        explicitness of trigger points.
123
124        In addition, you can put hooks in any point, rather than pre or post
125        of a method.
126
127    Q.  It looks interesting, but I just can't think of a practical example
128        of its use...
129
130    A.  (by Tony Bowden)
131
132        I originally added code like this to Class::DBI to cope with one
133        particular case: auto-upkeep of full-text search indices.
134
135        So I added functionality in Class::DBI to be able to trigger an
136        arbitary subroutine every time something happened - then it was a
137        simple matter of setting up triggers on INSERT and UPDATE to reindex
138        that row, and on DELETE to remove that index row.
139
140        See Class::DBI::mysql::FullTextSearch and its source code to see it
141        in action.
142
143AUTHOR
144    Original idea by Tony Bowden <tony@kasei.com> in Class::DBI.
145
146    Code by Tatsuhiko Miyagawa <miyagawa@bulknews.net>.
147
148    This library is free software; you can redistribute it and/or modify it
149    under the same terms as Perl itself.
150
151SEE ALSO
152    Class::DBI
153
154