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