bump version and update Changes for a release
[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
eca95e04 10our $VERSION = '0.78';
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
142subroutines within the particular package. We provide a very basic
86482605 143introspection interface.
fe122940 144
2eb717d5 145=head1 METHODS
146
de19f115 147=head2 Introspection
2eb717d5 148
de19f115 149=over 4
fe122940 150
2eb717d5 151=item B<meta>
152
32202ce2 153This will return a B<Class::MOP::Class> instance which is related
fe122940 154to this class.
155
2eb717d5 156=back
157
de19f115 158=head2 Construction
159
160=over 4
161
4c105333 162=item B<wrap ($code, %params)>
127d39a7 163
32202ce2 164This is the basic constructor, it returns a B<Class::MOP::Method>
f200954e 165instance which wraps the given C<$code> reference. You must also set
166the C<package_name> and C<name> attributes in C<%params>.
4c105333 167
168=item B<clone (%params)>
169
32202ce2 170This will make a copy of the object, allowing you to override
4c105333 171any values by stuffing them in C<%params>.
de19f115 172
de19f115 173=back
174
175=head2 Informational
176
177=over 4
178
7855ddba 179=item B<body>
180
127d39a7 181This returns the actual CODE reference of the particular instance.
182
de19f115 183=item B<name>
184
127d39a7 185This returns the name of the CODE reference.
186
5e607260 187=item B<associated_metaclass>
188
189The metaclass of the method
190
de19f115 191=item B<package_name>
192
127d39a7 193This returns the package name that the CODE reference is attached to.
194
96ceced8 195=item B<fully_qualified_name>
196
127d39a7 197This returns the fully qualified name of the CODE reference.
198
2226a8b0 199=item B<original_method>
200
201If this method object was created as a clone of some other method
202object, this returns the object that was cloned.
203
204=item B<original_name>
205
206This returns the original name of the CODE reference, wherever it was
207first defined.
208
2f011227 209If this method is a clone of a clone (of a clone, etc.), this method
210returns the name from the I<first> method in the chain of clones.
211
2226a8b0 212=item B<original_package_name>
213
214This returns the original package name that the CODE reference is
215attached to, wherever it was first defined.
216
2f011227 217If this method is a clone of a clone (of a clone, etc.), this method
218returns the package name from the I<first> method in the chain of
219clones.
220
2226a8b0 221=item B<original_fully_qualified_name>
222
223This returns the original fully qualified name of the CODE reference,
224wherever it was first defined.
225
2f011227 226If this method is a clone of a clone (of a clone, etc.), this method
227returns the fully qualified name from the I<first> method in the chain
228of clones.
229
96ceced8 230=back
231
5e607260 232=head2 Metaclass
233
234=over 4
235
236=item B<attach_to_class>
237
238Sets the associated metaclass
239
240=item B<detach_from_class>
241
242Disassociates the method from the metaclass
243
244=back
245
25a5f083 246=head2 Miscellaneous
247
248=over 4
249
250=item B<execute>
251
252Executes the method. Be sure to pass in the instance, since the
253method expects it.
254
255=back
256
1a09d9cc 257=head1 AUTHORS
8b978dd5 258
a2e85e6c 259Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 260
261=head1 COPYRIGHT AND LICENSE
262
69e3ab0a 263Copyright 2006-2008 by Infinity Interactive, Inc.
8b978dd5 264
265L<http://www.iinteractive.com>
266
267This library is free software; you can redistribute it and/or modify
32202ce2 268it under the same terms as Perl itself.
8b978dd5 269
16e960bd 270=cut
271