2 package Class::MOP::Method;
8 use Scalar::Util 'weaken';
10 our $VERSION = '0.81';
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;
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 ---------------------------------------------------------
33 my ( $class, @args ) = @_;
35 unshift @args, 'body' if @args % 2 == 1;
38 my $code = $params{body};
40 ('CODE' eq ref($code))
41 || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
43 ($params{package_name} && $params{name})
44 || confess "You must supply the package_name and name parameters $UPGRADE_ERROR_TEXT";
46 my $self = $class->_new(\%params);
48 weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
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},
67 sub associated_metaclass { shift->{'associated_metaclass'} }
70 my ( $self, $class ) = @_;
71 $self->{associated_metaclass} = $class;
72 weaken($self->{associated_metaclass});
75 sub detach_from_class {
77 delete $self->{associated_metaclass};
80 sub fully_qualified_name {
82 $self->package_name . '::' . $self->name;
85 sub original_method { (shift)->{'original_method'} }
87 sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
89 # It's possible that this could cause a loop if there is a circular
90 # reference in here. That shouldn't ever happen in normal
91 # circumstances, since original method only gets set when clone is
92 # called. We _could_ check for such a loop, but it'd involve some sort
93 # of package-lexical variable, and wouldn't be terribly subclassable.
94 sub original_package_name {
97 $self->original_method
98 ? $self->original_method->original_package_name
99 : $self->package_name;
105 $self->original_method
106 ? $self->original_method->original_name
110 sub original_fully_qualified_name {
113 $self->original_method
114 ? $self->original_method->original_fully_qualified_name
115 : $self->fully_qualified_name;
124 # the Class::MOP bootstrap
125 # will create this for us
137 Class::MOP::Method - Method Meta Object
141 The Method Protocol is very small, since methods in Perl 5 are just
142 subroutines in a specific package. We provide a very basic
143 introspection interface.
149 =item B<< Class::MOP::Method->wrap($code, %options) >>
151 This is the constructor. It accepts a subroutine reference and a hash
160 The method name (without a package name). This is required.
164 The package name for the method. This is required.
166 =item * associated_metaclass
168 An optional L<Class::MOP::Class> object. This is the metaclass for the
173 =item B<< $metamethod->clone(%params) >>
175 This makes a shallow clone of the method object. In particular,
176 subroutine reference itself is shared between all clones of a given
179 When a method is cloned, the original method object will be available
180 by calling C<original_method> on the clone.
182 =item B<< $metamethod->body >>
184 This returns a reference to the method's subroutine.
186 =item B<< $metamethod->name >>
188 This returns the method's name
190 =item B<< $metamethod->package_name >>
192 This returns the method's package name.
194 =item B<< $metamethod->fully_qualified_name >>
196 This returns the method's fully qualified name (package name and
199 =item B<< $metamethod->associated_metaclass >>
201 This returns the L<Class::MOP::Class> object for the method, if one
204 =item B<< $metamethod->original_method >>
206 If this method object was created as a clone of some other method
207 object, this returns the object that was cloned.
209 =item B<< $metamethod->original_name >>
211 This returns the method's original name, wherever it was first
214 If this method is a clone of a clone (of a clone, etc.), this method
215 returns the name from the I<first> method in the chain of clones.
217 =item B<< $metamethod->original_package_name >>
219 This returns the method's original package name, wherever it was first
222 If this method is a clone of a clone (of a clone, etc.), this method
223 returns the package name from the I<first> method in the chain of
226 =item B<< $metamethod->original_fully_qualified_name >>
228 This returns the method's original fully qualified name, wherever it
231 If this method is a clone of a clone (of a clone, etc.), this method
232 returns the fully qualified name from the I<first> method in the chain
235 =item B<< $metamethod->attach_to_class($metaclass) >>
237 Given a L<Class::MOP::Class> object, this method sets the associated
238 metaclass for the method. This will overwrite any existing associated
241 =item B<< $metamethod->detach_from_class >>
243 Removes any associated metaclass object for the method.
245 =item B<< $metamethod->execute(...) >>
247 This executes the method. Any arguments provided will be passed on to
250 =item B<< Class::MOP::Method->meta >>
252 This will return a L<Class::MOP::Class> instance for this class.
254 It should also be noted that L<Class::MOP> will actually bootstrap
255 this module by installing a number of attribute meta-objects into its
262 Stevan Little E<lt>stevan@iinteractive.comE<gt>
264 =head1 COPYRIGHT AND LICENSE
266 Copyright 2006-2009 by Infinity Interactive, Inc.
268 L<http://www.iinteractive.com>
270 This library is free software; you can redistribute it and/or modify
271 it under the same terms as Perl itself.