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