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