2 package Class::MOP::Method;
8 use Scalar::Util 'weaken', 'reftype';
10 our $VERSION = '0.89';
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 (ref $code && 'CODE' eq reftype($code))
32 || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
34 ($params{package_name} && $params{name})
35 || confess "You must supply the package_name and name parameters";
37 my $self = $class->_new(\%params);
39 weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
46 return Class::MOP::Class->initialize($class)->new_object(@_)
47 if $class ne __PACKAGE__;
49 my $params = @_ == 1 ? $_[0] : {@_};
52 'body' => $params->{body},
53 'associated_metaclass' => $params->{associated_metaclass},
54 'package_name' => $params->{package_name},
55 'name' => $params->{name},
56 'original_method' => $params->{original_method},
62 sub associated_metaclass { shift->{'associated_metaclass'} }
65 my ( $self, $class ) = @_;
66 $self->{associated_metaclass} = $class;
67 weaken($self->{associated_metaclass});
70 sub detach_from_class {
72 delete $self->{associated_metaclass};
75 sub fully_qualified_name {
77 $self->package_name . '::' . $self->name;
80 sub original_method { (shift)->{'original_method'} }
82 sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
84 # It's possible that this could cause a loop if there is a circular
85 # reference in here. That shouldn't ever happen in normal
86 # circumstances, since original method only gets set when clone is
87 # called. We _could_ check for such a loop, but it'd involve some sort
88 # of package-lexical variable, and wouldn't be terribly subclassable.
89 sub original_package_name {
92 $self->original_method
93 ? $self->original_method->original_package_name
94 : $self->package_name;
100 $self->original_method
101 ? $self->original_method->original_name
105 sub original_fully_qualified_name {
108 $self->original_method
109 ? $self->original_method->original_fully_qualified_name
110 : $self->fully_qualified_name;
119 # the Class::MOP bootstrap
120 # will create this for us
132 Class::MOP::Method - Method Meta Object
136 The Method Protocol is very small, since methods in Perl 5 are just
137 subroutines in a specific package. We provide a very basic
138 introspection interface.
144 =item B<< Class::MOP::Method->wrap($code, %options) >>
146 This is the constructor. It accepts a subroutine reference and a hash
155 The method name (without a package name). This is required.
159 The package name for the method. This is required.
161 =item * associated_metaclass
163 An optional L<Class::MOP::Class> object. This is the metaclass for the
168 =item B<< $metamethod->clone(%params) >>
170 This makes a shallow clone of the method object. In particular,
171 subroutine reference itself is shared between all clones of a given
174 When a method is cloned, the original method object will be available
175 by calling C<original_method> on the clone.
177 =item B<< $metamethod->body >>
179 This returns a reference to the method's subroutine.
181 =item B<< $metamethod->name >>
183 This returns the method's name
185 =item B<< $metamethod->package_name >>
187 This returns the method's package name.
189 =item B<< $metamethod->fully_qualified_name >>
191 This returns the method's fully qualified name (package name and
194 =item B<< $metamethod->associated_metaclass >>
196 This returns the L<Class::MOP::Class> object for the method, if one
199 =item B<< $metamethod->original_method >>
201 If this method object was created as a clone of some other method
202 object, this returns the object that was cloned.
204 =item B<< $metamethod->original_name >>
206 This returns the method's original name, wherever it was first
209 If this method is a clone of a clone (of a clone, etc.), this method
210 returns the name from the I<first> method in the chain of clones.
212 =item B<< $metamethod->original_package_name >>
214 This returns the method's original package name, wherever it was first
217 If this method is a clone of a clone (of a clone, etc.), this method
218 returns the package name from the I<first> method in the chain of
221 =item B<< $metamethod->original_fully_qualified_name >>
223 This returns the method's original fully qualified name, wherever it
226 If this method is a clone of a clone (of a clone, etc.), this method
227 returns the fully qualified name from the I<first> method in the chain
230 =item B<< $metamethod->attach_to_class($metaclass) >>
232 Given a L<Class::MOP::Class> object, this method sets the associated
233 metaclass for the method. This will overwrite any existing associated
236 =item B<< $metamethod->detach_from_class >>
238 Removes any associated metaclass object for the method.
240 =item B<< $metamethod->execute(...) >>
242 This executes the method. Any arguments provided will be passed on to
245 =item B<< Class::MOP::Method->meta >>
247 This will return a L<Class::MOP::Class> instance for this class.
249 It should also be noted that L<Class::MOP> will actually bootstrap
250 this module by installing a number of attribute meta-objects into its
257 Stevan Little E<lt>stevan@iinteractive.comE<gt>
259 =head1 COPYRIGHT AND LICENSE
261 Copyright 2006-2009 by Infinity Interactive, Inc.
263 L<http://www.iinteractive.com>
265 This library is free software; you can redistribute it and/or modify
266 it under the same terms as Perl itself.