2 package Class::MOP::Method;
8 use Scalar::Util 'weaken', 'reftype', 'blessed';
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Class::MOP::Object';
15 # if poked in the right way,
16 # they should act like CODE refs.
17 use overload '&{}' => sub { $_[0]->body }, fallback => 1;
22 my ( $class, @args ) = @_;
24 unshift @args, 'body' if @args % 2 == 1;
27 my $code = $params{body};
29 if (blessed($code) && $code->isa(__PACKAGE__)) {
30 my $method = $code->clone;
32 Class::MOP::class_of($class)->rebless_instance($method, %params);
35 elsif (!ref $code || 'CODE' ne reftype($code)) {
36 confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
39 ($params{package_name} && $params{name})
40 || confess "You must supply the package_name and name parameters";
42 my $self = $class->_new(\%params);
44 weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
52 return Class::MOP::Class->initialize($class)->new_object(@_)
53 if $class ne __PACKAGE__;
55 my $params = @_ == 1 ? $_[0] : {@_};
58 'body' => $params->{body},
59 'associated_metaclass' => $params->{associated_metaclass},
60 'package_name' => $params->{package_name},
61 'name' => $params->{name},
62 'original_method' => $params->{original_method},
68 sub associated_metaclass { shift->{'associated_metaclass'} }
71 my ( $self, $class ) = @_;
72 $self->{associated_metaclass} = $class;
73 weaken($self->{associated_metaclass});
76 sub detach_from_class {
78 delete $self->{associated_metaclass};
81 sub fully_qualified_name {
83 $self->package_name . '::' . $self->name;
86 sub original_method { (shift)->{'original_method'} }
88 sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
90 # It's possible that this could cause a loop if there is a circular
91 # reference in here. That shouldn't ever happen in normal
92 # circumstances, since original method only gets set when clone is
93 # called. We _could_ check for such a loop, but it'd involve some sort
94 # of package-lexical variable, and wouldn't be terribly subclassable.
95 sub original_package_name {
98 $self->original_method
99 ? $self->original_method->original_package_name
100 : $self->package_name;
106 $self->original_method
107 ? $self->original_method->original_name
111 sub original_fully_qualified_name {
114 $self->original_method
115 ? $self->original_method->original_fully_qualified_name
116 : $self->fully_qualified_name;
124 # We used to go through use Class::MOP::Class->clone_instance to do this, but
125 # this was awfully slow. This method may be called a number of times when
126 # classes are loaded (especially during Moose role application), so it is
127 # worth optimizing. - DR
131 my $clone = bless { %{$self}, @_ }, blessed($self);
133 $clone->_set_original_method($self);
140 # ABSTRACT: Method Meta Object
148 The Method Protocol is very small, since methods in Perl 5 are just
149 subroutines in a specific package. We provide a very basic
150 introspection interface.
156 =item B<< Class::MOP::Method->wrap($code, %options) >>
158 This is the constructor. It accepts a method body in the form of
159 either a code reference or a L<Class::MOP::Method> instance, followed
160 by a hash of options.
168 The method name (without a package name). This is required if C<$code>
173 The package name for the method. This is required if C<$code> is a
176 =item * associated_metaclass
178 An optional L<Class::MOP::Class> object. This is the metaclass for the
183 =item B<< $metamethod->clone(%params) >>
185 This makes a shallow clone of the method object. In particular,
186 subroutine reference itself is shared between all clones of a given
189 When a method is cloned, the original method object will be available
190 by calling C<original_method> on the clone.
192 =item B<< $metamethod->body >>
194 This returns a reference to the method's subroutine.
196 =item B<< $metamethod->name >>
198 This returns the method's name
200 =item B<< $metamethod->package_name >>
202 This returns the method's package name.
204 =item B<< $metamethod->fully_qualified_name >>
206 This returns the method's fully qualified name (package name and
209 =item B<< $metamethod->associated_metaclass >>
211 This returns the L<Class::MOP::Class> object for the method, if one
214 =item B<< $metamethod->original_method >>
216 If this method object was created as a clone of some other method
217 object, this returns the object that was cloned.
219 =item B<< $metamethod->original_name >>
221 This returns the method's original name, wherever it was first
224 If this method is a clone of a clone (of a clone, etc.), this method
225 returns the name from the I<first> method in the chain of clones.
227 =item B<< $metamethod->original_package_name >>
229 This returns the method's original package name, wherever it was first
232 If this method is a clone of a clone (of a clone, etc.), this method
233 returns the package name from the I<first> method in the chain of
236 =item B<< $metamethod->original_fully_qualified_name >>
238 This returns the method's original fully qualified name, wherever it
241 If this method is a clone of a clone (of a clone, etc.), this method
242 returns the fully qualified name from the I<first> method in the chain
245 =item B<< $metamethod->attach_to_class($metaclass) >>
247 Given a L<Class::MOP::Class> object, this method sets the associated
248 metaclass for the method. This will overwrite any existing associated
251 =item B<< $metamethod->detach_from_class >>
253 Removes any associated metaclass object for the method.
255 =item B<< $metamethod->execute(...) >>
257 This executes the method. Any arguments provided will be passed on to
260 =item B<< Class::MOP::Method->meta >>
262 This will return a L<Class::MOP::Class> instance for this class.
264 It should also be noted that L<Class::MOP> will actually bootstrap
265 this module by installing a number of attribute meta-objects into its