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