2 package Class::MOP::Method;
8 use Scalar::Util 'weaken', 'reftype', 'blessed';
10 our $VERSION = '1.06';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Object';
17 # if poked in the right way,
18 # they should act like CODE refs.
19 use overload '&{}' => sub { $_[0]->body }, fallback => 1;
24 my ( $class, @args ) = @_;
26 unshift @args, 'body' if @args % 2 == 1;
29 my $code = $params{body};
31 if (blessed($code) && $code->isa(__PACKAGE__)) {
32 my $method = $code->clone;
34 Class::MOP::class_of($class)->rebless_instance($method, %params);
37 elsif (!ref $code || 'CODE' ne reftype($code)) {
38 confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
41 ($params{package_name} && $params{name})
42 || confess "You must supply the package_name and name parameters";
44 my $self = $class->_new(\%params);
46 weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
54 return Class::MOP::Class->initialize($class)->new_object(@_)
55 if $class ne __PACKAGE__;
57 my $params = @_ == 1 ? $_[0] : {@_};
60 'body' => $params->{body},
61 'associated_metaclass' => $params->{associated_metaclass},
62 'package_name' => $params->{package_name},
63 'name' => $params->{name},
64 'original_method' => $params->{original_method},
70 sub associated_metaclass { shift->{'associated_metaclass'} }
73 my ( $self, $class ) = @_;
74 $self->{associated_metaclass} = $class;
75 weaken($self->{associated_metaclass});
78 sub detach_from_class {
80 delete $self->{associated_metaclass};
83 sub fully_qualified_name {
85 $self->package_name . '::' . $self->name;
88 sub original_method { (shift)->{'original_method'} }
90 sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
92 # It's possible that this could cause a loop if there is a circular
93 # reference in here. That shouldn't ever happen in normal
94 # circumstances, since original method only gets set when clone is
95 # called. We _could_ check for such a loop, but it'd involve some sort
96 # of package-lexical variable, and wouldn't be terribly subclassable.
97 sub original_package_name {
100 $self->original_method
101 ? $self->original_method->original_package_name
102 : $self->package_name;
108 $self->original_method
109 ? $self->original_method->original_name
113 sub original_fully_qualified_name {
116 $self->original_method
117 ? $self->original_method->original_fully_qualified_name
118 : $self->fully_qualified_name;
126 # We used to go through use Class::MOP::Class->clone_instance to do this, but
127 # this was awfully slow. This method may be called a number of times when
128 # classes are loaded (especially during Moose role application), so it is
129 # worth optimizing. - DR
133 my $clone = bless { %{$self}, @_ }, blessed($self);
135 $clone->_set_original_method($self);
148 Class::MOP::Method - Method Meta Object
152 The Method Protocol is very small, since methods in Perl 5 are just
153 subroutines in a specific package. We provide a very basic
154 introspection interface.
160 =item B<< Class::MOP::Method->wrap($code, %options) >>
162 This is the constructor. It accepts a method body in the form of
163 either a code reference or a L<Class::MOP::Method> instance, followed
164 by a hash of options.
172 The method name (without a package name). This is required if C<$code>
177 The package name for the method. This is required if C<$code> is a
180 =item * associated_metaclass
182 An optional L<Class::MOP::Class> object. This is the metaclass for the
187 =item B<< $metamethod->clone(%params) >>
189 This makes a shallow clone of the method object. In particular,
190 subroutine reference itself is shared between all clones of a given
193 When a method is cloned, the original method object will be available
194 by calling C<original_method> on the clone.
196 =item B<< $metamethod->body >>
198 This returns a reference to the method's subroutine.
200 =item B<< $metamethod->name >>
202 This returns the method's name
204 =item B<< $metamethod->package_name >>
206 This returns the method's package name.
208 =item B<< $metamethod->fully_qualified_name >>
210 This returns the method's fully qualified name (package name and
213 =item B<< $metamethod->associated_metaclass >>
215 This returns the L<Class::MOP::Class> object for the method, if one
218 =item B<< $metamethod->original_method >>
220 If this method object was created as a clone of some other method
221 object, this returns the object that was cloned.
223 =item B<< $metamethod->original_name >>
225 This returns the method's original name, wherever it was first
228 If this method is a clone of a clone (of a clone, etc.), this method
229 returns the name from the I<first> method in the chain of clones.
231 =item B<< $metamethod->original_package_name >>
233 This returns the method's original package name, wherever it was first
236 If this method is a clone of a clone (of a clone, etc.), this method
237 returns the package name from the I<first> method in the chain of
240 =item B<< $metamethod->original_fully_qualified_name >>
242 This returns the method's original fully qualified name, wherever it
245 If this method is a clone of a clone (of a clone, etc.), this method
246 returns the fully qualified name from the I<first> method in the chain
249 =item B<< $metamethod->attach_to_class($metaclass) >>
251 Given a L<Class::MOP::Class> object, this method sets the associated
252 metaclass for the method. This will overwrite any existing associated
255 =item B<< $metamethod->detach_from_class >>
257 Removes any associated metaclass object for the method.
259 =item B<< $metamethod->execute(...) >>
261 This executes the method. Any arguments provided will be passed on to
264 =item B<< Class::MOP::Method->meta >>
266 This will return a L<Class::MOP::Class> instance for this class.
268 It should also be noted that L<Class::MOP> will actually bootstrap
269 this module by installing a number of attribute meta-objects into its
276 Stevan Little E<lt>stevan@iinteractive.comE<gt>
278 =head1 COPYRIGHT AND LICENSE
280 Copyright 2006-2010 by Infinity Interactive, Inc.
282 L<http://www.iinteractive.com>
284 This library is free software; you can redistribute it and/or modify
285 it under the same terms as Perl itself.