2 package Class::MOP::Method;
8 use Scalar::Util 'weaken', 'reftype', 'blessed';
10 use base 'Class::MOP::Object';
13 # if poked in the right way,
14 # they should act like CODE refs.
15 use overload '&{}' => sub { $_[0]->body }, fallback => 1;
20 my ( $class, @args ) = @_;
22 unshift @args, 'body' if @args % 2 == 1;
25 my $code = $params{body};
27 if (blessed($code) && $code->isa(__PACKAGE__)) {
28 my $method = $code->clone;
30 Class::MOP::class_of($class)->rebless_instance($method, %params);
33 elsif (!ref $code || 'CODE' ne reftype($code)) {
34 confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
37 ($params{package_name} && $params{name})
38 || confess "You must supply the package_name and name parameters";
40 my $self = $class->_new(\%params);
42 weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
50 return Class::MOP::Class->initialize($class)->new_object(@_)
51 if $class ne __PACKAGE__;
53 my $params = @_ == 1 ? $_[0] : {@_};
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},
66 sub associated_metaclass { shift->{'associated_metaclass'} }
69 my ( $self, $class ) = @_;
70 $self->{associated_metaclass} = $class;
71 weaken($self->{associated_metaclass});
74 sub detach_from_class {
76 delete $self->{associated_metaclass};
79 sub fully_qualified_name {
81 $self->package_name . '::' . $self->name;
84 sub original_method { (shift)->{'original_method'} }
86 sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
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.
93 sub original_package_name {
96 $self->original_method
97 ? $self->original_method->original_package_name
98 : $self->package_name;
104 $self->original_method
105 ? $self->original_method->original_name
109 sub original_fully_qualified_name {
112 $self->original_method
113 ? $self->original_method->original_fully_qualified_name
114 : $self->fully_qualified_name;
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
129 my $clone = bless { %{$self}, @_ }, blessed($self);
131 $clone->_set_original_method($self);
138 # ABSTRACT: Method Meta Object
146 The Method Protocol is very small, since methods in Perl 5 are just
147 subroutines in a specific package. We provide a very basic
148 introspection interface.
154 =item B<< Class::MOP::Method->wrap($code, %options) >>
156 This is the constructor. It accepts a method body in the form of
157 either a code reference or a L<Class::MOP::Method> instance, followed
158 by a hash of options.
166 The method name (without a package name). This is required if C<$code>
171 The package name for the method. This is required if C<$code> is a
174 =item * associated_metaclass
176 An optional L<Class::MOP::Class> object. This is the metaclass for the
181 =item B<< $metamethod->clone(%params) >>
183 This makes a shallow clone of the method object. In particular,
184 subroutine reference itself is shared between all clones of a given
187 When a method is cloned, the original method object will be available
188 by calling C<original_method> on the clone.
190 =item B<< $metamethod->body >>
192 This returns a reference to the method's subroutine.
194 =item B<< $metamethod->name >>
196 This returns the method's name
198 =item B<< $metamethod->package_name >>
200 This returns the method's package name.
202 =item B<< $metamethod->fully_qualified_name >>
204 This returns the method's fully qualified name (package name and
207 =item B<< $metamethod->associated_metaclass >>
209 This returns the L<Class::MOP::Class> object for the method, if one
212 =item B<< $metamethod->original_method >>
214 If this method object was created as a clone of some other method
215 object, this returns the object that was cloned.
217 =item B<< $metamethod->original_name >>
219 This returns the method's original name, wherever it was first
222 If this method is a clone of a clone (of a clone, etc.), this method
223 returns the name from the I<first> method in the chain of clones.
225 =item B<< $metamethod->original_package_name >>
227 This returns the method's original package name, wherever it was first
230 If this method is a clone of a clone (of a clone, etc.), this method
231 returns the package name from the I<first> method in the chain of
234 =item B<< $metamethod->original_fully_qualified_name >>
236 This returns the method's original fully qualified name, wherever it
239 If this method is a clone of a clone (of a clone, etc.), this method
240 returns the fully qualified name from the I<first> method in the chain
243 =item B<< $metamethod->attach_to_class($metaclass) >>
245 Given a L<Class::MOP::Class> object, this method sets the associated
246 metaclass for the method. This will overwrite any existing associated
249 =item B<< $metamethod->detach_from_class >>
251 Removes any associated metaclass object for the method.
253 =item B<< $metamethod->execute(...) >>
255 This executes the method. Any arguments provided will be passed on to
258 =item B<< Class::MOP::Method->meta >>
260 This will return a L<Class::MOP::Class> instance for this class.
262 It should also be noted that L<Class::MOP> will actually bootstrap
263 this module by installing a number of attribute meta-objects into its