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