2 package Class::MOP::Method;
8 use Scalar::Util 'weaken';
10 our $VERSION = '0.71_02';
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 body { (shift)->{'body'} }
69 sub associated_metaclass { shift->{'associated_metaclass'} }
72 my ( $self, $class ) = @_;
73 $self->{associated_metaclass} = $class;
74 weaken($self->{associated_metaclass});
77 sub detach_from_class {
79 delete $self->{associated_metaclass};
82 sub package_name { (shift)->{'package_name'} }
84 sub name { (shift)->{'name'} }
86 sub fully_qualified_name {
88 $self->package_name . '::' . $self->name;
91 sub original_method { (shift)->{'original_method'} }
93 sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
95 # It's possible that this could cause a loop if there is a circular
96 # reference in here. That shouldn't ever happen in normal
97 # circumstances, since original method only gets set when clone is
98 # called. We _could_ check for such a loop, but it'd involve some sort
99 # of package-lexical variable, and wouldn't be terribly subclassable.
100 sub original_package_name {
103 $self->original_method
104 ? $self->original_method->original_package_name
105 : $self->package_name;
111 $self->original_method
112 ? $self->original_method->original_name
116 sub original_fully_qualified_name {
119 $self->original_method
120 ? $self->original_method->original_fully_qualified_name
121 : $self->fully_qualified_name;
130 # the Class::MOP bootstrap
131 # will create this for us
143 Class::MOP::Method - Method Meta Object
147 The Method Protocol is very small, since methods in Perl 5 are just
148 subroutines within the particular package. We provide a very basic
149 introspection interface.
159 This will return a B<Class::MOP::Class> instance which is related
168 =item B<wrap ($code, %params)>
170 This is the basic constructor, it returns a B<Class::MOP::Method>
171 instance which wraps the given C<$code> reference. You can also
172 set the C<package_name> and C<name> attributes using the C<%params>.
173 If these are not set, then thier accessors will attempt to figure
174 it out using the C<Class::MOP::get_code_info> function.
176 =item B<clone (%params)>
178 This will make a copy of the object, allowing you to override
179 any values by stuffing them in C<%params>.
189 This returns the actual CODE reference of the particular instance.
193 This returns the name of the CODE reference.
195 =item B<associated_metaclass>
197 The metaclass of the method
199 =item B<package_name>
201 This returns the package name that the CODE reference is attached to.
203 =item B<fully_qualified_name>
205 This returns the fully qualified name of the CODE reference.
207 =item B<original_method>
209 If this method object was created as a clone of some other method
210 object, this returns the object that was cloned.
212 =item B<original_name>
214 This returns the original name of the CODE reference, wherever it was
217 If this method is a clone of a clone (of a clone, etc.), this method
218 returns the name from the I<first> method in the chain of clones.
220 =item B<original_package_name>
222 This returns the original package name that the CODE reference is
223 attached to, wherever it was first defined.
225 If this method is a clone of a clone (of a clone, etc.), this method
226 returns the package name from the I<first> method in the chain of
229 =item B<original_fully_qualified_name>
231 This returns the original fully qualified name of the CODE reference,
232 wherever it was first defined.
234 If this method is a clone of a clone (of a clone, etc.), this method
235 returns the fully qualified name from the I<first> method in the chain
244 =item B<attach_to_class>
246 Sets the associated metaclass
248 =item B<detach_from_class>
250 Disassociates the method from the metaclass
260 Executes the method. Be sure to pass in the instance, since the
267 Stevan Little E<lt>stevan@iinteractive.comE<gt>
269 =head1 COPYRIGHT AND LICENSE
271 Copyright 2006-2008 by Infinity Interactive, Inc.
273 L<http://www.iinteractive.com>
275 This library is free software; you can redistribute it and/or modify
276 it under the same terms as Perl itself.