Commit | Line | Data |
8b978dd5 |
1 | |
2 | package Class::MOP::Method; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
2eb717d5 |
7 | use Carp 'confess'; |
6db5c459 |
8 | use Scalar::Util 'weaken', 'reftype', 'blessed'; |
2eb717d5 |
9 | |
67f694ad |
10 | our $VERSION = '1.04'; |
d519662a |
11 | $VERSION = eval $VERSION; |
f0480c45 |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
de19f115 |
13 | |
b1897d4d |
14 | use base 'Class::MOP::Object'; |
15 | |
ce2ae40f |
16 | # NOTE: |
32202ce2 |
17 | # if poked in the right way, |
ce2ae40f |
18 | # they should act like CODE refs. |
c23184fc |
19 | use overload '&{}' => sub { $_[0]->body }, fallback => 1; |
7855ddba |
20 | |
de19f115 |
21 | # construction |
22 | |
32202ce2 |
23 | sub wrap { |
5caf45ce |
24 | my ( $class, @args ) = @_; |
25 | |
26 | unshift @args, 'body' if @args % 2 == 1; |
27 | |
28 | my %params = @args; |
29 | my $code = $params{body}; |
32202ce2 |
30 | |
6db5c459 |
31 | if (blessed($code) && $code->isa(__PACKAGE__)) { |
32 | my $method = $code->clone; |
33 | delete $params{body}; |
34 | Class::MOP::class_of($class)->rebless_instance($method, %params); |
35 | return $method; |
36 | } |
37 | elsif (!ref $code || 'CODE' ne reftype($code)) { |
38 | confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")"; |
39 | } |
32202ce2 |
40 | |
b38f3848 |
41 | ($params{package_name} && $params{name}) |
e7cfb28b |
42 | || confess "You must supply the package_name and name parameters"; |
32202ce2 |
43 | |
c2829bbc |
44 | my $self = $class->_new(\%params); |
5e607260 |
45 | |
46 | weaken($self->{associated_metaclass}) if $self->{associated_metaclass}; |
47 | |
48 | return $self; |
de19f115 |
49 | } |
50 | |
71b98d4f |
51 | sub _new { |
0bfc85b8 |
52 | my $class = shift; |
812d58f9 |
53 | |
ec9e38e5 |
54 | return Class::MOP::Class->initialize($class)->new_object(@_) |
812d58f9 |
55 | if $class ne __PACKAGE__; |
ec9e38e5 |
56 | |
0bfc85b8 |
57 | my $params = @_ == 1 ? $_[0] : {@_}; |
71b98d4f |
58 | |
ec9e38e5 |
59 | return bless { |
0bfc85b8 |
60 | 'body' => $params->{body}, |
61 | 'associated_metaclass' => $params->{associated_metaclass}, |
62 | 'package_name' => $params->{package_name}, |
63 | 'name' => $params->{name}, |
ec9e38e5 |
64 | 'original_method' => $params->{original_method}, |
71b98d4f |
65 | } => $class; |
66 | } |
67 | |
ce2ae40f |
68 | ## accessors |
69 | |
5e607260 |
70 | sub associated_metaclass { shift->{'associated_metaclass'} } |
b1897d4d |
71 | |
5e607260 |
72 | sub attach_to_class { |
73 | my ( $self, $class ) = @_; |
74 | $self->{associated_metaclass} = $class; |
75 | weaken($self->{associated_metaclass}); |
76 | } |
77 | |
78 | sub detach_from_class { |
79 | my $self = shift; |
80 | delete $self->{associated_metaclass}; |
81 | } |
de19f115 |
82 | |
96ceced8 |
83 | sub fully_qualified_name { |
91b73829 |
84 | my $self = shift; |
85 | $self->package_name . '::' . $self->name; |
96ceced8 |
86 | } |
87 | |
2226a8b0 |
88 | sub original_method { (shift)->{'original_method'} } |
89 | |
90 | sub _set_original_method { $_[0]->{'original_method'} = $_[1] } |
91 | |
92 | # It's possible that this could cause a loop if there is a circular |
93 | # reference in here. That shouldn't ever happen in normal |
94 | # circumstances, since original method only gets set when clone is |
95 | # called. We _could_ check for such a loop, but it'd involve some sort |
96 | # of package-lexical variable, and wouldn't be terribly subclassable. |
97 | sub original_package_name { |
98 | my $self = shift; |
99 | |
100 | $self->original_method |
101 | ? $self->original_method->original_package_name |
102 | : $self->package_name; |
103 | } |
104 | |
105 | sub original_name { |
106 | my $self = shift; |
107 | |
108 | $self->original_method |
109 | ? $self->original_method->original_name |
110 | : $self->name; |
111 | } |
112 | |
113 | sub original_fully_qualified_name { |
114 | my $self = shift; |
115 | |
116 | $self->original_method |
117 | ? $self->original_method->original_fully_qualified_name |
118 | : $self->fully_qualified_name; |
119 | } |
120 | |
25a5f083 |
121 | sub execute { |
122 | my $self = shift; |
123 | $self->body->(@_); |
124 | } |
125 | |
0a3388ad |
126 | # We used to go through use Class::MOP::Class->clone_instance to do this, but |
127 | # this was awfully slow. This method may be called a number of times when |
128 | # classes are loaded (especially during Moose role application), so it is |
129 | # worth optimizing. - DR |
130 | sub clone { |
131 | my $self = shift; |
132 | |
133 | my $clone = bless { %{$self}, @_ }, blessed($self); |
134 | |
135 | $clone->_set_original_method($self); |
136 | |
137 | return $clone; |
138 | } |
4c105333 |
139 | |
8b978dd5 |
140 | 1; |
141 | |
142 | __END__ |
143 | |
144 | =pod |
145 | |
32202ce2 |
146 | =head1 NAME |
8b978dd5 |
147 | |
148 | Class::MOP::Method - Method Meta Object |
149 | |
8b978dd5 |
150 | =head1 DESCRIPTION |
151 | |
32202ce2 |
152 | The Method Protocol is very small, since methods in Perl 5 are just |
8a0b1202 |
153 | subroutines in a specific package. We provide a very basic |
86482605 |
154 | introspection interface. |
fe122940 |
155 | |
2eb717d5 |
156 | =head1 METHODS |
157 | |
de19f115 |
158 | =over 4 |
fe122940 |
159 | |
8a0b1202 |
160 | =item B<< Class::MOP::Method->wrap($code, %options) >> |
2eb717d5 |
161 | |
9fd0e686 |
162 | This is the constructor. It accepts a method body in the form of |
163 | either a code reference or a L<Class::MOP::Method> instance, followed |
164 | by a hash of options. |
fe122940 |
165 | |
8a0b1202 |
166 | The options are: |
2eb717d5 |
167 | |
8a0b1202 |
168 | =over 8 |
de19f115 |
169 | |
8a0b1202 |
170 | =item * name |
171 | |
9fd0e686 |
172 | The method name (without a package name). This is required if C<$code> |
173 | is a coderef. |
de19f115 |
174 | |
8a0b1202 |
175 | =item * package_name |
127d39a7 |
176 | |
9fd0e686 |
177 | The package name for the method. This is required if C<$code> is a |
178 | coderef. |
4c105333 |
179 | |
8a0b1202 |
180 | =item * associated_metaclass |
4c105333 |
181 | |
8a0b1202 |
182 | An optional L<Class::MOP::Class> object. This is the metaclass for the |
183 | method's class. |
de19f115 |
184 | |
de19f115 |
185 | =back |
186 | |
8a0b1202 |
187 | =item B<< $metamethod->clone(%params) >> |
de19f115 |
188 | |
8a0b1202 |
189 | This makes a shallow clone of the method object. In particular, |
190 | subroutine reference itself is shared between all clones of a given |
191 | method. |
192 | |
193 | When a method is cloned, the original method object will be available |
194 | by calling C<original_method> on the clone. |
de19f115 |
195 | |
8a0b1202 |
196 | =item B<< $metamethod->body >> |
7855ddba |
197 | |
8a0b1202 |
198 | This returns a reference to the method's subroutine. |
127d39a7 |
199 | |
8a0b1202 |
200 | =item B<< $metamethod->name >> |
de19f115 |
201 | |
8a0b1202 |
202 | This returns the method's name |
127d39a7 |
203 | |
8a0b1202 |
204 | =item B<< $metamethod->package_name >> |
5e607260 |
205 | |
8a0b1202 |
206 | This returns the method's package name. |
5e607260 |
207 | |
8a0b1202 |
208 | =item B<< $metamethod->fully_qualified_name >> |
de19f115 |
209 | |
8a0b1202 |
210 | This returns the method's fully qualified name (package name and |
211 | method name). |
127d39a7 |
212 | |
8a0b1202 |
213 | =item B<< $metamethod->associated_metaclass >> |
96ceced8 |
214 | |
8a0b1202 |
215 | This returns the L<Class::MOP::Class> object for the method, if one |
216 | exists. |
127d39a7 |
217 | |
8a0b1202 |
218 | =item B<< $metamethod->original_method >> |
2226a8b0 |
219 | |
220 | If this method object was created as a clone of some other method |
221 | object, this returns the object that was cloned. |
222 | |
8a0b1202 |
223 | =item B<< $metamethod->original_name >> |
2226a8b0 |
224 | |
8a0b1202 |
225 | This returns the method's original name, wherever it was first |
226 | defined. |
2226a8b0 |
227 | |
2f011227 |
228 | If this method is a clone of a clone (of a clone, etc.), this method |
229 | returns the name from the I<first> method in the chain of clones. |
230 | |
8a0b1202 |
231 | =item B<< $metamethod->original_package_name >> |
2226a8b0 |
232 | |
8a0b1202 |
233 | This returns the method's original package name, wherever it was first |
234 | defined. |
2226a8b0 |
235 | |
2f011227 |
236 | If this method is a clone of a clone (of a clone, etc.), this method |
237 | returns the package name from the I<first> method in the chain of |
238 | clones. |
239 | |
8a0b1202 |
240 | =item B<< $metamethod->original_fully_qualified_name >> |
2226a8b0 |
241 | |
8a0b1202 |
242 | This returns the method's original fully qualified name, wherever it |
243 | was first defined. |
2226a8b0 |
244 | |
2f011227 |
245 | If this method is a clone of a clone (of a clone, etc.), this method |
246 | returns the fully qualified name from the I<first> method in the chain |
247 | of clones. |
248 | |
8a0b1202 |
249 | =item B<< $metamethod->attach_to_class($metaclass) >> |
96ceced8 |
250 | |
8a0b1202 |
251 | Given a L<Class::MOP::Class> object, this method sets the associated |
252 | metaclass for the method. This will overwrite any existing associated |
253 | metaclass. |
5e607260 |
254 | |
8a0b1202 |
255 | =item B<< $metamethod->detach_from_class >> |
5e607260 |
256 | |
8a0b1202 |
257 | Removes any associated metaclass object for the method. |
5e607260 |
258 | |
8a0b1202 |
259 | =item B<< $metamethod->execute(...) >> |
5e607260 |
260 | |
8a0b1202 |
261 | This executes the method. Any arguments provided will be passed on to |
262 | the method itself. |
25a5f083 |
263 | |
8a0b1202 |
264 | =item B<< Class::MOP::Method->meta >> |
25a5f083 |
265 | |
8a0b1202 |
266 | This will return a L<Class::MOP::Class> instance for this class. |
25a5f083 |
267 | |
8a0b1202 |
268 | It should also be noted that L<Class::MOP> will actually bootstrap |
269 | this module by installing a number of attribute meta-objects into its |
270 | metaclass. |
25a5f083 |
271 | |
272 | =back |
273 | |
1a09d9cc |
274 | =head1 AUTHORS |
8b978dd5 |
275 | |
a2e85e6c |
276 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
8b978dd5 |
277 | |
278 | =head1 COPYRIGHT AND LICENSE |
279 | |
3e2c8600 |
280 | Copyright 2006-2010 by Infinity Interactive, Inc. |
8b978dd5 |
281 | |
282 | L<http://www.iinteractive.com> |
283 | |
284 | This library is free software; you can redistribute it and/or modify |
32202ce2 |
285 | it under the same terms as Perl itself. |
8b978dd5 |
286 | |
16e960bd |
287 | =cut |
288 | |