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