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