root/Class-Trigger/trunk/README

Revision 403 (checked in by miyagawa, 19 years ago)

add

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 NAME
2     Class::Trigger - Mix-in to add / call inheritable triggers
3
4 SYNOPSIS
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 DESCRIPTION
29     Class::Trigger is a mix-in class to add / call triggers (or hooks) that
30     get called at some points you specify.
31
32 METHODS
33     By using this module, your class is capable of following two methods.
34
35     add_trigger
36           Foo->add_trigger($triggerpoint => $sub);
37           $foo->add_trigger($triggerpoint => $sub);
38
39         Adds triggers for trigger point. You can have any number of triggers
40         for each point. Each coderef will be passed a copy of the object,
41         and return values will be ignored.
42
43         If "add_trigger" is called as object method, whole trigger table
44         will be copied onto the object. Then the object should be
45         implemented as hash.
46
47           my $foo = Foo->new;
48
49           # this trigger ($sub_foo) would apply only to $foo object
50           $foo->add_trigger($triggerpoint => $sub_foo);
51           $foo->foo;
52
53           # And not to another $bar object
54           my $bar = Foo->new;
55           $bar->foo;
56
57     call_trigger
58           $foo->call_trigger($triggerpoint);
59
60         Calls triggers for trigger point, which were added via "add_trigger"
61         method. Each triggers will be passed a copy of the object.
62
63 TRIGGER POINTS
64     By default you can make any number of trigger points, but if you want to
65     declare names of trigger points explicitly, you can do it via "import".
66
67       package Foo;
68       use Class::Trigger qw(foo bar baz);
69
70       package main;
71       Foo->add_trigger(foo  => \&sub1); # okay
72       Foo->add_trigger(hoge => \&sub2); # exception
73
74 AUTHOR
75     Original idea by Tony Bowden <tony@kasei.com> in Class::DBI.
76
77     Code by Tatsuhiko Miyagawa <miyagawa@bulknews.net>.
78
79     This library is free software; you can redistribute it and/or modify it
80     under the same terms as Perl itself.
81
82 SEE ALSO
83     the Class::Data::Inheritable manpage
84
Note: See TracBrowser for help on using the browser.