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