Add release date
[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
83e7706f 10our $VERSION = '0.71';
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
0bfc85b8 46 my $self = (ref($class) || $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
8683db0e 67sub body { (shift)->{'body'} }
7855ddba 68
5e607260 69sub associated_metaclass { shift->{'associated_metaclass'} }
b1897d4d 70
5e607260 71sub attach_to_class {
72 my ( $self, $class ) = @_;
73 $self->{associated_metaclass} = $class;
74 weaken($self->{associated_metaclass});
75}
76
77sub detach_from_class {
78 my $self = shift;
79 delete $self->{associated_metaclass};
80}
de19f115 81
da88f307 82sub package_name { (shift)->{'package_name'} }
de19f115 83
da88f307 84sub name { (shift)->{'name'} }
de19f115 85
96ceced8 86sub fully_qualified_name {
91b73829 87 my $self = shift;
88 $self->package_name . '::' . $self->name;
96ceced8 89}
90
2226a8b0 91sub original_method { (shift)->{'original_method'} }
92
93sub _set_original_method { $_[0]->{'original_method'} = $_[1] }
94
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.
100sub original_package_name {
101 my $self = shift;
102
103 $self->original_method
104 ? $self->original_method->original_package_name
105 : $self->package_name;
106}
107
108sub original_name {
109 my $self = shift;
110
111 $self->original_method
112 ? $self->original_method->original_name
113 : $self->name;
114}
115
116sub original_fully_qualified_name {
117 my $self = shift;
118
119 $self->original_method
120 ? $self->original_method->original_fully_qualified_name
121 : $self->fully_qualified_name;
122}
123
4c105333 124# NOTE:
125# the Class::MOP bootstrap
126# will create this for us
127# - SL
128# sub clone { ... }
129
8b978dd5 1301;
131
132__END__
133
134=pod
135
32202ce2 136=head1 NAME
8b978dd5 137
138Class::MOP::Method - Method Meta Object
139
8b978dd5 140=head1 DESCRIPTION
141
32202ce2 142The Method Protocol is very small, since methods in Perl 5 are just
143subroutines within the particular package. We provide a very basic
86482605 144introspection interface.
fe122940 145
2eb717d5 146=head1 METHODS
147
de19f115 148=head2 Introspection
2eb717d5 149
de19f115 150=over 4
fe122940 151
2eb717d5 152=item B<meta>
153
32202ce2 154This will return a B<Class::MOP::Class> instance which is related
fe122940 155to this class.
156
2eb717d5 157=back
158
de19f115 159=head2 Construction
160
161=over 4
162
4c105333 163=item B<wrap ($code, %params)>
127d39a7 164
32202ce2 165This is the basic constructor, it returns a B<Class::MOP::Method>
166instance which wraps the given C<$code> reference. You can also
4c105333 167set the C<package_name> and C<name> attributes using the C<%params>.
32202ce2 168If these are not set, then thier accessors will attempt to figure
4c105333 169it out using the C<Class::MOP::get_code_info> function.
170
171=item B<clone (%params)>
172
32202ce2 173This will make a copy of the object, allowing you to override
4c105333 174any values by stuffing them in C<%params>.
de19f115 175
de19f115 176=back
177
178=head2 Informational
179
180=over 4
181
7855ddba 182=item B<body>
183
127d39a7 184This returns the actual CODE reference of the particular instance.
185
de19f115 186=item B<name>
187
127d39a7 188This returns the name of the CODE reference.
189
5e607260 190=item B<associated_metaclass>
191
192The metaclass of the method
193
de19f115 194=item B<package_name>
195
127d39a7 196This returns the package name that the CODE reference is attached to.
197
96ceced8 198=item B<fully_qualified_name>
199
127d39a7 200This returns the fully qualified name of the CODE reference.
201
2226a8b0 202=item B<original_method>
203
204If this method object was created as a clone of some other method
205object, this returns the object that was cloned.
206
207=item B<original_name>
208
209This returns the original name of the CODE reference, wherever it was
210first defined.
211
2f011227 212If this method is a clone of a clone (of a clone, etc.), this method
213returns the name from the I<first> method in the chain of clones.
214
2226a8b0 215=item B<original_package_name>
216
217This returns the original package name that the CODE reference is
218attached to, wherever it was first defined.
219
2f011227 220If this method is a clone of a clone (of a clone, etc.), this method
221returns the package name from the I<first> method in the chain of
222clones.
223
2226a8b0 224=item B<original_fully_qualified_name>
225
226This returns the original fully qualified name of the CODE reference,
227wherever it was first defined.
228
2f011227 229If this method is a clone of a clone (of a clone, etc.), this method
230returns the fully qualified name from the I<first> method in the chain
231of clones.
232
96ceced8 233=back
234
5e607260 235=head2 Metaclass
236
237=over 4
238
239=item B<attach_to_class>
240
241Sets the associated metaclass
242
243=item B<detach_from_class>
244
245Disassociates the method from the metaclass
246
247=back
248
1a09d9cc 249=head1 AUTHORS
8b978dd5 250
a2e85e6c 251Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 252
253=head1 COPYRIGHT AND LICENSE
254
69e3ab0a 255Copyright 2006-2008 by Infinity Interactive, Inc.
8b978dd5 256
257L<http://www.iinteractive.com>
258
259This library is free software; you can redistribute it and/or modify
32202ce2 260it under the same terms as Perl itself.
8b978dd5 261
16e960bd 262=cut
263