1Class::Method::ModifierUss(e3r)Contributed Perl DocumentCaltaisosn::Method::Modifiers(3)
2
3
4
6 Class::Method::Modifiers - provides Moose-like method modifiers
7
9 package Child;
10 use parent 'Parent';
11 use Class::Method::Modifiers;
12
13 sub new_method { }
14
15 before 'old_method' => sub {
16 carp "old_method is deprecated, use new_method";
17 };
18
19 around 'other_method' => sub {
20 my $orig = shift;
21 my $ret = $orig->(@_);
22 return $ret =~ /\d/ ? $ret : lc $ret;
23 };
24
25 after 'private', 'protected' => sub {
26 debug "finished calling a dangerous method";
27 };
28
30 Method modifiers are a convenient feature from the CLOS (Common Lisp
31 Object System) world.
32
33 In its most basic form, a method modifier is just a method that calls
34 "$self->SUPER::foo(@_)". I for one have trouble remembering that exact
35 invocation, so my classes seldom re-dispatch to their base classes.
36 Very bad!
37
38 "Class::Method::Modifiers" provides three modifiers: "before",
39 "around", and "after". "before" and "after" are run just before and
40 after the method they modify, but can not really affect that original
41 method. "around" is run in place of the original method, with a hook to
42 easily call that original method. See the "MODIFIERS" section for more
43 details on how the particular modifiers work.
44
45 One clear benefit of using "Class::Method::Modifiers" is that you can
46 define multiple modifiers in a single namespace. These separate
47 modifiers don't need to know about each other. This makes top-down
48 design easy. Have a base class that provides the skeleton methods of
49 each operation, and have plugins modify those methods to flesh out the
50 specifics.
51
52 Parent classes need not know about "Class::Method::Modifiers". This
53 means you should be able to modify methods in any subclass. See
54 Term::VT102::ZeroBased for an example of subclassing with CMM.
55
56 In short, "Class::Method::Modifiers" solves the problem of making sure
57 you call "$self->SUPER::foo(@_)", and provides a cleaner interface for
58 it.
59
60 As of version 1.00, "Class::Method::Modifiers" is faster in some cases
61 than Moose. See "benchmark/method_modifiers.pl" in the Moose
62 distribution.
63
65 before method(s) => sub { ... }
66 "before" is called before the method it is modifying. Its return value
67 is totally ignored. It receives the same @_ as the the method it is
68 modifying would have received. You can modify the @_ the original
69 method will receive by changing $_[0] and friends (or by changing
70 anything inside a reference). This is a feature!
71
72 after method(s) => sub { ... }
73 "after" is called after the method it is modifying. Its return value is
74 totally ignored. It receives the same @_ as the the method it is
75 modifying received, mostly. The original method can modify @_ (such as
76 by changing $_[0] or references) and "after" will see the modified
77 version. If you don't like this behavior, specify both a "before" and
78 "after", and copy the @_ during "before" for "after" to use.
79
80 around method(s) => sub { ... }
81 "around" is called instead of the method it is modifying. The method
82 you're overriding is passed in as the first argument (called $orig by
83 convention). Watch out for contextual return values of $orig.
84
85 You can use "around" to:
86
87 Pass $orig a different @_
88 around 'method' => sub {
89 my $orig = shift;
90 my $self = shift;
91 $orig->($self, reverse @_);
92 };
93
94 Munge the return value of $orig
95 around 'method' => sub {
96 my $orig = shift;
97 ucfirst $orig->(@_);
98 };
99
100 Avoid calling $orig -- conditionally
101 around 'method' => sub {
102 my $orig = shift;
103 return $orig->(@_) if time() % 2;
104 return "no dice, captain";
105 };
106
108 All three normal modifiers; "before", "after", and "around"; are
109 exported into your namespace by default. You may "use
110 Class::Method::Modifiers ()" to avoid thrashing your namespace. I may
111 steal more features from Moose, namely "super", "override", "inner",
112 "augment", and whatever the Moose folks come up with next.
113
114 Note that the syntax and semantics for these modifiers is directly
115 borrowed from Moose (the implementations, however, are not).
116
117 Class::Trigger shares a few similarities with
118 "Class::Method::Modifiers", and they even have some overlap in purpose
119 -- both can be used to implement highly pluggable applications. The
120 difference is that Class::Trigger provides a mechanism for easily
121 letting parent classes to invoke hooks defined by other code.
122 "Class::Method::Modifiers" provides a way of overriding/augmenting
123 methods safely, and the parent class need not know about it.
124
126 It is erroneous to modify a method that doesn't exist in your class's
127 inheritance hierarchy. If this occurs, an exception will be thrown when
128 the modifier is defined.
129
130 It doesn't yet play well with "caller". There are some todo tests for
131 this. Don't get your hopes up though!
132
134 This module was bumped to 1.00 following a complete reimplementation,
135 to indicate breaking backwards compatibility. The "guard" modifier was
136 removed, and the internals are completely different.
137
138 The new version is a few times faster with half the code. It's now even
139 faster than Moose.
140
141 Any code that just used modifiers should not change in behavior, except
142 to become more correct. And, of course, faster. :)
143
145 Class::Method::Modifiers::Fast Moose, Class::Trigger,
146 Class::MOP::Method::Wrapped, MRO::Compat, CLOS
147
149 Shawn M Moore, "sartak@gmail.com"
150
152 Thanks to Stevan Little for Moose, I would never have known about
153 method modifiers otherwise.
154
155 Thanks to Matt Trout and Stevan Little for their advice.
156
158 Copyright 2007-2009 Shawn M Moore.
159
160 This program is free software; you can redistribute it and/or modify it
161 under the same terms as Perl itself.
162
163
164
165perl v5.12.3 2011-02-24 Class::Method::Modifiers(3)