bump version to 0.80_01
[gitmo/Class-MOP.git] / lib / Class / MOP / Method.pm
CommitLineData
8b978dd5 1
2package Class::MOP::Method;
3
4use strict;
5use warnings;
6
2eb717d5 7use Carp 'confess';
5e607260 8use Scalar::Util 'weaken';
2eb717d5 9
b92a5299 10our $VERSION = '0.80_01';
d519662a 11$VERSION = eval $VERSION;
f0480c45 12our $AUTHORITY = 'cpan:STEVAN';
de19f115 13
b1897d4d 14use base 'Class::MOP::Object';
15
ce2ae40f 16# NOTE:
32202ce2 17# if poked in the right way,
ce2ae40f 18# they should act like CODE refs.
c23184fc 19use overload '&{}' => sub { $_[0]->body }, fallback => 1;
7855ddba 20
32202ce2 21our $UPGRADE_ERROR_TEXT = q{
22---------------------------------------------------------
23NOTE: this error is likely not an error, but a regression
24caused by the latest upgrade to Moose/Class::MOP. Consider
25upgrading any MooseX::* modules to their latest versions
26before spending too much time chasing this one down.
27---------------------------------------------------------
28};
29
de19f115 30# construction
31
32202ce2 32sub wrap {
5caf45ce 33 my ( $class, @args ) = @_;
34
35 unshift @args, 'body' if @args % 2 == 1;
36
37 my %params = @args;
38 my $code = $params{body};
32202ce2 39
9b522fc4 40 ('CODE' eq ref($code))
4d47b77f 41 || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
32202ce2 42
b38f3848 43 ($params{package_name} && $params{name})
32202ce2 44 || confess "You must supply the package_name and name parameters $UPGRADE_ERROR_TEXT";
45
c2829bbc 46 my $self = $class->_new(\%params);
5e607260 47
48 weaken($self->{associated_metaclass}) if $self->{associated_metaclass};
49
50 return $self;
de19f115 51}
52
71b98d4f 53sub _new {
0bfc85b8 54 my $class = shift;
55 my $params = @_ == 1 ? $_[0] : {@_};
71b98d4f 56
57 my $self = bless {
0bfc85b8 58 'body' => $params->{body},
59 'associated_metaclass' => $params->{associated_metaclass},
60 'package_name' => $params->{package_name},
61 'name' => $params->{name},
71b98d4f 62 } => $class;
63}
64
ce2ae40f 65## accessors
66
5e607260 67sub associated_metaclass { shift->{'associated_metaclass'} }
b1897d4d 68
5e607260 69sub attach_to_class {
70 my ( $self, $class ) = @_;
71 $self->{associated_metaclass} = $class;
72 weaken($self->{associated_metaclass});
73}
74
75sub detach_from_class {
76 my $self = shift;
77 delete $self->{associated_metaclass};
78}
de19f115 79
96ceced8 80sub fully_qualified_name {
91b73829 81 my $self = shift;
82 $self->package_name . '::' . $self->name;
96ceced8 83}
84
2226a8b0 85sub original_method { (shift)->{'original_method'} }
86
87sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
88
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.
94sub original_package_name {
95 my $self = shift;
96
97 $self->original_method
98 ? $self->original_method->original_package_name
99 : $self->package_name;
100}
101
102sub original_name {
103 my $self = shift;
104
105 $self->original_method
106 ? $self->original_method->original_name
107 : $self->name;
108}
109
110sub original_fully_qualified_name {
111 my $self = shift;
112
113 $self->original_method
114 ? $self->original_method->original_fully_qualified_name
115 : $self->fully_qualified_name;
116}
117
25a5f083 118sub execute {
119 my $self = shift;
120 $self->body->(@_);
121}
122
4c105333 123# NOTE:
124# the Class::MOP bootstrap
125# will create this for us
126# - SL
127# sub clone { ... }
128
8b978dd5 1291;
130
131__END__
132
133=pod
134
32202ce2 135=head1 NAME
8b978dd5 136
137Class::MOP::Method - Method Meta Object
138
8b978dd5 139=head1 DESCRIPTION
140
32202ce2 141The Method Protocol is very small, since methods in Perl 5 are just
8a0b1202 142subroutines in a specific package. We provide a very basic
86482605 143introspection interface.
fe122940 144
2eb717d5 145=head1 METHODS
146
de19f115 147=over 4
fe122940 148
8a0b1202 149=item B<< Class::MOP::Method->wrap($code, %options) >>
2eb717d5 150
8a0b1202 151This is the constructor. It accepts a subroutine reference and a hash
152of options.
fe122940 153
8a0b1202 154The options are:
2eb717d5 155
8a0b1202 156=over 8
de19f115 157
8a0b1202 158=item * name
159
160The method name (without a package name). This is required.
de19f115 161
8a0b1202 162=item * package_name
127d39a7 163
8a0b1202 164The package name for the method. This is required.
4c105333 165
8a0b1202 166=item * associated_metaclass
4c105333 167
8a0b1202 168An optional L<Class::MOP::Class> object. This is the metaclass for the
169method's class.
de19f115 170
de19f115 171=back
172
8a0b1202 173=item B<< $metamethod->clone(%params) >>
de19f115 174
8a0b1202 175This makes a shallow clone of the method object. In particular,
176subroutine reference itself is shared between all clones of a given
177method.
178
179When a method is cloned, the original method object will be available
180by calling C<original_method> on the clone.
de19f115 181
8a0b1202 182=item B<< $metamethod->body >>
7855ddba 183
8a0b1202 184This returns a reference to the method's subroutine.
127d39a7 185
8a0b1202 186=item B<< $metamethod->name >>
de19f115 187
8a0b1202 188This returns the method's name
127d39a7 189
8a0b1202 190=item B<< $metamethod->package_name >>
5e607260 191
8a0b1202 192This returns the method's package name.
5e607260 193
8a0b1202 194=item B<< $metamethod->fully_qualified_name >>
de19f115 195
8a0b1202 196This returns the method's fully qualified name (package name and
197method name).
127d39a7 198
8a0b1202 199=item B<< $metamethod->associated_metaclass >>
96ceced8 200
8a0b1202 201This returns the L<Class::MOP::Class> object for the method, if one
202exists.
127d39a7 203
8a0b1202 204=item B<< $metamethod->original_method >>
2226a8b0 205
206If this method object was created as a clone of some other method
207object, this returns the object that was cloned.
208
8a0b1202 209=item B<< $metamethod->original_name >>
2226a8b0 210
8a0b1202 211This returns the method's original name, wherever it was first
212defined.
2226a8b0 213
2f011227 214If this method is a clone of a clone (of a clone, etc.), this method
215returns the name from the I<first> method in the chain of clones.
216
8a0b1202 217=item B<< $metamethod->original_package_name >>
2226a8b0 218
8a0b1202 219This returns the method's original package name, wherever it was first
220defined.
2226a8b0 221
2f011227 222If this method is a clone of a clone (of a clone, etc.), this method
223returns the package name from the I<first> method in the chain of
224clones.
225
8a0b1202 226=item B<< $metamethod->original_fully_qualified_name >>
2226a8b0 227
8a0b1202 228This returns the method's original fully qualified name, wherever it
229was first defined.
2226a8b0 230
2f011227 231If this method is a clone of a clone (of a clone, etc.), this method
232returns the fully qualified name from the I<first> method in the chain
233of clones.
234
8a0b1202 235=item B<< $metamethod->attach_to_class($metaclass) >>
96ceced8 236
8a0b1202 237Given a L<Class::MOP::Class> object, this method sets the associated
238metaclass for the method. This will overwrite any existing associated
239metaclass.
5e607260 240
8a0b1202 241=item B<< $metamethod->detach_from_class >>
5e607260 242
8a0b1202 243Removes any associated metaclass object for the method.
5e607260 244
8a0b1202 245=item B<< $metamethod->execute(...) >>
5e607260 246
8a0b1202 247This executes the method. Any arguments provided will be passed on to
248the method itself.
25a5f083 249
8a0b1202 250=item B<< Class::MOP::Method->meta >>
25a5f083 251
8a0b1202 252This will return a L<Class::MOP::Class> instance for this class.
25a5f083 253
8a0b1202 254It should also be noted that L<Class::MOP> will actually bootstrap
255this module by installing a number of attribute meta-objects into its
256metaclass.
25a5f083 257
258=back
259
1a09d9cc 260=head1 AUTHORS
8b978dd5 261
a2e85e6c 262Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 263
264=head1 COPYRIGHT AND LICENSE
265
070bb6c9 266Copyright 2006-2009 by Infinity Interactive, Inc.
8b978dd5 267
268L<http://www.iinteractive.com>
269
270This library is free software; you can redistribute it and/or modify
32202ce2 271it under the same terms as Perl itself.
8b978dd5 272
16e960bd 273=cut
274