home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Trigger.pm < prev    next >
Encoding:
Perl POD Document  |  2003-02-27  |  6.9 KB  |  279 lines

  1. package Class::Trigger;
  2.  
  3. use strict;
  4. use vars qw($VERSION);
  5. $VERSION = 0.08;
  6.  
  7. use Class::Data::Inheritable;
  8. use Carp ();
  9.  
  10. sub import {
  11.     my $class = shift;
  12.     my $pkg = caller(0);
  13.  
  14.     # XXX 5.005_03 isa() is broken with MI
  15.     unless ($pkg->can('mk_classdata')) {
  16.     no strict 'refs';
  17.     push @{"$pkg\::ISA"}, 'Class::Data::Inheritable';
  18.     }
  19.  
  20.     $pkg->mk_classdata('__triggers');
  21.     $pkg->mk_classdata('__triggerpoints');
  22.  
  23.     $pkg->__triggerpoints({ map { $_ => 1 } @_ }) if @_;
  24.  
  25.     # export mixin methods
  26.     no strict 'refs';
  27.     my @methods = qw(add_trigger call_trigger);
  28.     *{"$pkg\::$_"} = \&{$_} for @methods;
  29. }
  30.  
  31. sub add_trigger {
  32.     my $proto = shift;
  33.  
  34.     # should be deep copy of the hash: for inheritance
  35.     my $old_triggers = __fetch_triggers($proto) || {};
  36.     my %triggers = __deep_dereference($old_triggers);
  37.     while (my($when, $code) = splice @_, 0, 2) {
  38.     __validate_triggerpoint($proto, $when);
  39.     Carp::croak('add_trigger() needs coderef') unless ref($code) eq 'CODE';
  40.     push @{$triggers{$when}}, $code;
  41.     }
  42.     __update_triggers($proto, \%triggers);
  43. }
  44.  
  45. sub call_trigger {
  46.     my $self = shift;
  47.     my $all_triggers = __fetch_triggers($self) || return; # any triggers?
  48.     my $when = shift;
  49.     if (my $triggers = $all_triggers->{$when}) {
  50.     for my $trigger (@$triggers) {
  51.         $trigger->($self, @_);
  52.     }
  53.     }
  54.     else {
  55.     # if validation is enabled we can only add valid trigger points
  56.     # so we only need to check in call_trigger() if there's no
  57.     # trigger with the requested name.
  58.     __validate_triggerpoint($self, $when);
  59.     }
  60. }
  61.  
  62. sub __validate_triggerpoint {
  63.     my $points = $_[0]->__triggerpoints || return;
  64.     my ($self, $when) = @_;
  65.     Carp::croak("$when is not valid triggerpoint for ".(ref($self) || $self))
  66.     unless $points->{$when};
  67. }
  68.  
  69. sub __fetch_triggers {
  70.     my $proto = shift;
  71.     # check object based triggers first
  72.     return (ref $proto and $proto->{__triggers}) || $proto->__triggers;
  73. }
  74.  
  75. sub __update_triggers {
  76.     my($proto, $triggers) = @_;
  77.     if (ref $proto) {
  78.     # object attributes
  79.     $proto->{__triggers} = $triggers;
  80.     }
  81.     else {
  82.     # class data inheritable
  83.     $proto->__triggers($triggers);
  84.     }
  85. }
  86.  
  87. sub __deep_dereference {
  88.     my $hashref = shift;
  89.     my %copy;
  90.     while (my($key, $arrayref) = each %$hashref) {
  91.     $copy{$key} = [ @$arrayref ];
  92.     }
  93.     return %copy;
  94. }
  95.  
  96. 1;
  97. __END__
  98.  
  99. =head1 NAME
  100.  
  101. Class::Trigger - Mixin to add / call inheritable triggers
  102.  
  103. =head1 SYNOPSIS
  104.  
  105.   package Foo;
  106.   use Class::Trigger;
  107.  
  108.   sub foo {
  109.       my $self = shift;
  110.       $self->call_trigger('before_foo');
  111.       # some code ...
  112.       $self->call_trigger('middle_of_foo');
  113.       # some code ...
  114.       $self->call_trigger('after_foo');
  115.   }
  116.  
  117.   package main;
  118.   Foo->add_trigger(before_foo => \&sub1);
  119.   Foo->add_trigger(after_foo => \&sub2);
  120.  
  121.   my $foo = Foo->new;
  122.   $foo->foo;            # then sub1, sub2 called
  123.  
  124.   # triggers are inheritable
  125.   package Bar;
  126.   use base qw(Foo);
  127.  
  128.   Bar->add_trigger(before_foo => \&sub);
  129.  
  130.   # triggers can be object based
  131.   $foo->add_trigger(after_foo => \&sub3);
  132.   $foo->foo;            # sub3 would appply only to this object
  133.  
  134. =head1 DESCRIPTION
  135.  
  136. Class::Trigger is a mixin class to add / call triggers (or hooks)
  137. that get called at some points you specify.
  138.  
  139. =head1 METHODS
  140.  
  141. By using this module, your class is capable of following two methods.
  142.  
  143. =over 4
  144.  
  145. =item add_trigger
  146.  
  147.   Foo->add_trigger($triggerpoint => $sub);
  148.   $foo->add_trigger($triggerpoint => $sub);
  149.  
  150. Adds triggers for trigger point. You can have any number of triggers
  151. for each point. Each coderef will be passed a the object reference, and
  152. return values will be ignored.
  153.  
  154. If C<add_trigger> is called as object method, whole current trigger
  155. table will be copied onto the object and the new trigger added to
  156. that. (The object must be implemented as hash.)
  157.  
  158.   my $foo = Foo->new;
  159.  
  160.   # this trigger ($sub_foo) would apply only to $foo object
  161.   $foo->add_trigger($triggerpoint => $sub_foo);
  162.   $foo->foo;
  163.  
  164.   # And not to another $bar object
  165.   my $bar = Foo->new;
  166.   $bar->foo;
  167.  
  168. Any triggers added to the class after adding a trigger to an object
  169. will not be fired for the object because the object now has a private
  170. copy of the triggers.
  171.  
  172.  
  173. =item call_trigger
  174.  
  175.   $foo->call_trigger($triggerpoint);
  176.  
  177. Calls triggers for trigger point, which were added via C<add_trigger>
  178. method. Each triggers will be passed a copy of the object.
  179. Triggers are invoked in the same order they were defined.
  180.  
  181. =back
  182.  
  183. =head1 TRIGGER POINTS
  184.  
  185. By default you can make any number of trigger points, but if you want
  186. to declare names of trigger points explicitly, you can do it via
  187. C<import>.
  188.  
  189.   package Foo;
  190.   use Class::Trigger qw(foo bar baz);
  191.  
  192.   package main;
  193.   Foo->add_trigger(foo  => \&sub1); # okay
  194.   Foo->add_trigger(hoge => \&sub2); # exception
  195.  
  196. =head1 FAQ
  197.  
  198. B<Acknowledgement:> Thanks to everyone at POOP mailing-list
  199. (http://poop.sourceforge.net/).
  200.  
  201. =over 4
  202.  
  203. =item Q.
  204.  
  205. This module lets me add subs to be run before/after a specific
  206. subroutine is run.  Yes?
  207.  
  208. =item A.
  209.  
  210. You put various call_trigger() method in your class.  Then your class
  211. users can call add_trigger() method to add subs to be run in points
  212. just you specify (exactly where you put call_trigger()).
  213.  
  214. =item Q.
  215.  
  216. Are you aware of the perl-aspects project and the Aspect module?  Very
  217. similar to Class::Trigger by the look of it, but its not nearly as
  218. explicit.  Its not necessary for foo() to actually say "triggers go
  219. *here*", you just add them.
  220.  
  221. =item A.
  222.  
  223. Yep ;)
  224.  
  225. But the difference with Aspect would be that Class::Trigger is so
  226. simple that it's easy to learn, and doesn't require 5.6 or over.
  227.  
  228. =item Q.
  229.  
  230. How does this compare to Sub::Versive, or Hook::LexWrap?
  231.  
  232. =item A.
  233.  
  234. Very similar. But the difference with Class::Trigger would be the
  235. explicitness of trigger points.
  236.  
  237. In addition, you can put hooks in any point, rather than pre or post
  238. of a method.
  239.  
  240. =item Q.
  241.  
  242. It looks interesting, but I just can't think of a practical example of
  243. its use...
  244.  
  245. =item A.
  246.  
  247. (by Tony Bowden)
  248.  
  249. I originally added code like this to Class::DBI to cope with one
  250. particular case: auto-upkeep of full-text search indices.
  251.  
  252. So I added functionality in Class::DBI to be able to trigger an
  253. arbitary subroutine every time something happened - then it was a
  254. simple matter of setting up triggers on INSERT and UPDATE to reindex
  255. that row, and on DELETE to remove that index row.
  256.  
  257. See L<Class::DBI::mysql::FullTextSearch> and its source code to see it
  258. in action.
  259.  
  260. =back
  261.  
  262. =head1 AUTHOR
  263.  
  264. Original idea by Tony Bowden E<lt>tony@kasei.comE<gt> in Class::DBI.
  265.  
  266. Code by Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>.
  267.  
  268. Patches by Tim Buce E<lt>Tim.Bunce@pobox.comE<gt>.
  269.  
  270. This library is free software; you can redistribute it and/or modify
  271. it under the same terms as Perl itself.
  272.  
  273. =head1 SEE ALSO
  274.  
  275. L<Class::Data::Inheritable>
  276.  
  277. =cut
  278.  
  279.