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 | |
b4bd10ec |
10 | our $VERSION = '0.77'; |
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 | |
8683db0e |
67 | sub body { (shift)->{'body'} } |
7855ddba |
68 | |
5e607260 |
69 | sub associated_metaclass { shift->{'associated_metaclass'} } |
b1897d4d |
70 | |
5e607260 |
71 | sub attach_to_class { |
72 | my ( $self, $class ) = @_; |
73 | $self->{associated_metaclass} = $class; |
74 | weaken($self->{associated_metaclass}); |
75 | } |
76 | |
77 | sub detach_from_class { |
78 | my $self = shift; |
79 | delete $self->{associated_metaclass}; |
80 | } |
de19f115 |
81 | |
da88f307 |
82 | sub package_name { (shift)->{'package_name'} } |
de19f115 |
83 | |
da88f307 |
84 | sub name { (shift)->{'name'} } |
de19f115 |
85 | |
96ceced8 |
86 | sub fully_qualified_name { |
91b73829 |
87 | my $self = shift; |
88 | $self->package_name . '::' . $self->name; |
96ceced8 |
89 | } |
90 | |
2226a8b0 |
91 | sub original_method { (shift)->{'original_method'} } |
92 | |
93 | sub _set_original_method { $_[0]->{'original_method'} = $_[1] } |
94 | |
95 | # It's possible that this could cause a loop if there is a circular |
96 | # reference in here. That shouldn't ever happen in normal |
97 | # circumstances, since original method only gets set when clone is |
98 | # called. We _could_ check for such a loop, but it'd involve some sort |
99 | # of package-lexical variable, and wouldn't be terribly subclassable. |
100 | sub original_package_name { |
101 | my $self = shift; |
102 | |
103 | $self->original_method |
104 | ? $self->original_method->original_package_name |
105 | : $self->package_name; |
106 | } |
107 | |
108 | sub original_name { |
109 | my $self = shift; |
110 | |
111 | $self->original_method |
112 | ? $self->original_method->original_name |
113 | : $self->name; |
114 | } |
115 | |
116 | sub original_fully_qualified_name { |
117 | my $self = shift; |
118 | |
119 | $self->original_method |
120 | ? $self->original_method->original_fully_qualified_name |
121 | : $self->fully_qualified_name; |
122 | } |
123 | |
25a5f083 |
124 | sub execute { |
125 | my $self = shift; |
126 | $self->body->(@_); |
127 | } |
128 | |
4c105333 |
129 | # NOTE: |
130 | # the Class::MOP bootstrap |
131 | # will create this for us |
132 | # - SL |
133 | # sub clone { ... } |
134 | |
8b978dd5 |
135 | 1; |
136 | |
137 | __END__ |
138 | |
139 | =pod |
140 | |
32202ce2 |
141 | =head1 NAME |
8b978dd5 |
142 | |
143 | Class::MOP::Method - Method Meta Object |
144 | |
8b978dd5 |
145 | =head1 DESCRIPTION |
146 | |
32202ce2 |
147 | The Method Protocol is very small, since methods in Perl 5 are just |
148 | subroutines within the particular package. We provide a very basic |
86482605 |
149 | introspection interface. |
fe122940 |
150 | |
2eb717d5 |
151 | =head1 METHODS |
152 | |
de19f115 |
153 | =head2 Introspection |
2eb717d5 |
154 | |
de19f115 |
155 | =over 4 |
fe122940 |
156 | |
2eb717d5 |
157 | =item B<meta> |
158 | |
32202ce2 |
159 | This will return a B<Class::MOP::Class> instance which is related |
fe122940 |
160 | to this class. |
161 | |
2eb717d5 |
162 | =back |
163 | |
de19f115 |
164 | =head2 Construction |
165 | |
166 | =over 4 |
167 | |
4c105333 |
168 | =item B<wrap ($code, %params)> |
127d39a7 |
169 | |
32202ce2 |
170 | This is the basic constructor, it returns a B<Class::MOP::Method> |
f200954e |
171 | instance which wraps the given C<$code> reference. You must also set |
172 | the C<package_name> and C<name> attributes in C<%params>. |
4c105333 |
173 | |
174 | =item B<clone (%params)> |
175 | |
32202ce2 |
176 | This will make a copy of the object, allowing you to override |
4c105333 |
177 | any values by stuffing them in C<%params>. |
de19f115 |
178 | |
de19f115 |
179 | =back |
180 | |
181 | =head2 Informational |
182 | |
183 | =over 4 |
184 | |
7855ddba |
185 | =item B<body> |
186 | |
127d39a7 |
187 | This returns the actual CODE reference of the particular instance. |
188 | |
de19f115 |
189 | =item B<name> |
190 | |
127d39a7 |
191 | This returns the name of the CODE reference. |
192 | |
5e607260 |
193 | =item B<associated_metaclass> |
194 | |
195 | The metaclass of the method |
196 | |
de19f115 |
197 | =item B<package_name> |
198 | |
127d39a7 |
199 | This returns the package name that the CODE reference is attached to. |
200 | |
96ceced8 |
201 | =item B<fully_qualified_name> |
202 | |
127d39a7 |
203 | This returns the fully qualified name of the CODE reference. |
204 | |
2226a8b0 |
205 | =item B<original_method> |
206 | |
207 | If this method object was created as a clone of some other method |
208 | object, this returns the object that was cloned. |
209 | |
210 | =item B<original_name> |
211 | |
212 | This returns the original name of the CODE reference, wherever it was |
213 | first defined. |
214 | |
2f011227 |
215 | If this method is a clone of a clone (of a clone, etc.), this method |
216 | returns the name from the I<first> method in the chain of clones. |
217 | |
2226a8b0 |
218 | =item B<original_package_name> |
219 | |
220 | This returns the original package name that the CODE reference is |
221 | attached to, wherever it was first defined. |
222 | |
2f011227 |
223 | If this method is a clone of a clone (of a clone, etc.), this method |
224 | returns the package name from the I<first> method in the chain of |
225 | clones. |
226 | |
2226a8b0 |
227 | =item B<original_fully_qualified_name> |
228 | |
229 | This returns the original fully qualified name of the CODE reference, |
230 | wherever it was first defined. |
231 | |
2f011227 |
232 | If this method is a clone of a clone (of a clone, etc.), this method |
233 | returns the fully qualified name from the I<first> method in the chain |
234 | of clones. |
235 | |
96ceced8 |
236 | =back |
237 | |
5e607260 |
238 | =head2 Metaclass |
239 | |
240 | =over 4 |
241 | |
242 | =item B<attach_to_class> |
243 | |
244 | Sets the associated metaclass |
245 | |
246 | =item B<detach_from_class> |
247 | |
248 | Disassociates the method from the metaclass |
249 | |
250 | =back |
251 | |
25a5f083 |
252 | =head2 Miscellaneous |
253 | |
254 | =over 4 |
255 | |
256 | =item B<execute> |
257 | |
258 | Executes the method. Be sure to pass in the instance, since the |
259 | method expects it. |
260 | |
261 | =back |
262 | |
1a09d9cc |
263 | =head1 AUTHORS |
8b978dd5 |
264 | |
a2e85e6c |
265 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
8b978dd5 |
266 | |
267 | =head1 COPYRIGHT AND LICENSE |
268 | |
69e3ab0a |
269 | Copyright 2006-2008 by Infinity Interactive, Inc. |
8b978dd5 |
270 | |
271 | L<http://www.iinteractive.com> |
272 | |
273 | This library is free software; you can redistribute it and/or modify |
32202ce2 |
274 | it under the same terms as Perl itself. |
8b978dd5 |
275 | |
16e960bd |
276 | =cut |
277 | |