Commit | Line | Data |
94b19069 |
1 | |
2 | package Class::MOP; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
3cf322a0 |
7 | use MRO::Compat; |
8 | |
727919c5 |
9 | use Carp 'confess'; |
be7677c7 |
10 | use Scalar::Util 'weaken'; |
8b978dd5 |
11 | |
2eb717d5 |
12 | use Class::MOP::Class; |
13 | use Class::MOP::Attribute; |
14 | use Class::MOP::Method; |
15 | |
c23184fc |
16 | use Class::MOP::Immutable; |
857f87a7 |
17 | |
b1f5f41d |
18 | BEGIN { |
c50b27ea |
19 | our $VERSION = '0.56'; |
b1f5f41d |
20 | our $AUTHORITY = 'cpan:STEVAN'; |
21 | |
22 | use XSLoader; |
23 | XSLoader::load( 'Class::MOP', $VERSION ); |
24 | |
11b56828 |
25 | *IS_RUNNING_ON_5_10 = ($] < 5.009_005) |
26 | ? sub () { 0 } |
27 | : sub () { 1 }; |
28 | |
29 | # get it from MRO::Compat now ... |
30 | *check_package_cache_flag = \&mro::get_pkg_gen; |
46b23b44 |
31 | |
32 | # UNCOMMENT ME TO TEST WITHOUT XS |
33 | #no warnings 'prototype', 'redefine'; |
34 | #*check_package_cache_flag = \&MRO::Compat::__get_pkg_gen_pp |
b1f5f41d |
35 | } |
e0e4674a |
36 | |
be7677c7 |
37 | { |
38 | # Metaclasses are singletons, so we cache them here. |
39 | # there is no need to worry about destruction though |
40 | # because they should die only when the program dies. |
41 | # After all, do package definitions even get reaped? |
1d68af04 |
42 | my %METAS; |
43 | |
44 | # means of accessing all the metaclasses that have |
be7677c7 |
45 | # been initialized thus far (for mugwumps obj browser) |
1d68af04 |
46 | sub get_all_metaclasses { %METAS } |
47 | sub get_all_metaclass_instances { values %METAS } |
48 | sub get_all_metaclass_names { keys %METAS } |
be7677c7 |
49 | sub get_metaclass_by_name { $METAS{$_[0]} } |
1d68af04 |
50 | sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] } |
51 | sub weaken_metaclass { weaken($METAS{$_[0]}) } |
be7677c7 |
52 | sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} } |
1d68af04 |
53 | sub remove_metaclass_by_name { $METAS{$_[0]} = undef } |
54 | |
be7677c7 |
55 | # NOTE: |
1d68af04 |
56 | # We only cache metaclasses, meaning instances of |
57 | # Class::MOP::Class. We do not cache instance of |
be7677c7 |
58 | # Class::MOP::Package or Class::MOP::Module. Mostly |
1d68af04 |
59 | # because I don't yet see a good reason to do so. |
be7677c7 |
60 | } |
61 | |
448b6e55 |
62 | sub load_class { |
63 | my $class = shift; |
1d68af04 |
64 | # see if this is already |
448b6e55 |
65 | # loaded in the symbol table |
66 | return 1 if is_class_loaded($class); |
67 | # otherwise require it ... |
68 | my $file = $class . '.pm'; |
69 | $file =~ s{::}{/}g; |
70 | eval { CORE::require($file) }; |
71 | confess "Could not load class ($class) because : $@" if $@; |
72 | unless (does_metaclass_exist($class)) { |
73 | eval { Class::MOP::Class->initialize($class) }; |
1d68af04 |
74 | confess "Could not initialize class ($class) because : $@" if $@; |
448b6e55 |
75 | } |
76 | 1; # return true if it worked |
77 | } |
78 | |
79 | sub is_class_loaded { |
c1d5345a |
80 | my $class = shift; |
81 | no strict 'refs'; |
82 | return 1 if defined ${"${class}::VERSION"} || defined @{"${class}::ISA"}; |
8861fab2 |
83 | foreach my $symbol (keys %{"${class}::"}) { |
84 | next if substr($symbol, -2, 2) eq '::'; |
85 | return 1 if defined &{"${class}::${symbol}"}; |
c1d5345a |
86 | } |
87 | return 0; |
448b6e55 |
88 | } |
89 | |
90 | |
aa448b16 |
91 | ## ---------------------------------------------------------------------------- |
92 | ## Setting up our environment ... |
93 | ## ---------------------------------------------------------------------------- |
1d68af04 |
94 | ## Class::MOP needs to have a few things in the global perl environment so |
aa448b16 |
95 | ## that it can operate effectively. Those things are done here. |
96 | ## ---------------------------------------------------------------------------- |
97 | |
3bf7644b |
98 | # ... nothing yet actually ;) |
8b978dd5 |
99 | |
b51af7f9 |
100 | ## ---------------------------------------------------------------------------- |
1d68af04 |
101 | ## Bootstrapping |
b51af7f9 |
102 | ## ---------------------------------------------------------------------------- |
1d68af04 |
103 | ## The code below here is to bootstrap our MOP with itself. This is also |
b51af7f9 |
104 | ## sometimes called "tying the knot". By doing this, we make it much easier |
105 | ## to extend the MOP through subclassing and such since now you can use the |
1d68af04 |
106 | ## MOP itself to extend itself. |
107 | ## |
b51af7f9 |
108 | ## Yes, I know, thats weird and insane, but it's a good thing, trust me :) |
1d68af04 |
109 | ## ---------------------------------------------------------------------------- |
727919c5 |
110 | |
1d68af04 |
111 | # We need to add in the meta-attributes here so that |
112 | # any subclass of Class::MOP::* will be able to |
727919c5 |
113 | # inherit them using &construct_instance |
114 | |
f0480c45 |
115 | ## -------------------------------------------------------- |
6d5355c3 |
116 | ## Class::MOP::Package |
727919c5 |
117 | |
6d5355c3 |
118 | Class::MOP::Package->meta->add_attribute( |
c23184fc |
119 | Class::MOP::Attribute->new('$!package' => ( |
b880e0de |
120 | reader => { |
1d68af04 |
121 | # NOTE: we need to do this in order |
122 | # for the instance meta-object to |
b880e0de |
123 | # not fall into meta-circular death |
1d68af04 |
124 | # |
ce2ae40f |
125 | # we just alias the original method |
1d68af04 |
126 | # rather than re-produce it here |
ce2ae40f |
127 | 'name' => \&Class::MOP::Package::name |
b880e0de |
128 | }, |
c23184fc |
129 | init_arg => 'package', |
727919c5 |
130 | )) |
131 | ); |
132 | |
a5e51f0b |
133 | Class::MOP::Package->meta->add_attribute( |
c23184fc |
134 | Class::MOP::Attribute->new('%!namespace' => ( |
a5e51f0b |
135 | reader => { |
56dcfc1a |
136 | # NOTE: |
ce2ae40f |
137 | # we just alias the original method |
138 | # rather than re-produce it here |
139 | 'namespace' => \&Class::MOP::Package::namespace |
a5e51f0b |
140 | }, |
2e877f58 |
141 | init_arg => undef, |
c4260b45 |
142 | default => sub { \undef } |
a5e51f0b |
143 | )) |
144 | ); |
145 | |
9d6dce77 |
146 | # NOTE: |
147 | # use the metaclass to construct the meta-package |
148 | # which is a superclass of the metaclass itself :P |
149 | Class::MOP::Package->meta->add_method('initialize' => sub { |
150 | my $class = shift; |
151 | my $package_name = shift; |
1d68af04 |
152 | $class->meta->new_object('package' => $package_name, @_); |
9d6dce77 |
153 | }); |
154 | |
f0480c45 |
155 | ## -------------------------------------------------------- |
156 | ## Class::MOP::Module |
157 | |
158 | # NOTE: |
1d68af04 |
159 | # yeah this is kind of stretching things a bit, |
f0480c45 |
160 | # but truthfully the version should be an attribute |
1d68af04 |
161 | # of the Module, the weirdness comes from having to |
162 | # stick to Perl 5 convention and store it in the |
163 | # $VERSION package variable. Basically if you just |
164 | # squint at it, it will look how you want it to look. |
f0480c45 |
165 | # Either as a package variable, or as a attribute of |
166 | # the metaclass, isn't abstraction great :) |
167 | |
168 | Class::MOP::Module->meta->add_attribute( |
c23184fc |
169 | Class::MOP::Attribute->new('$!version' => ( |
f0480c45 |
170 | reader => { |
ce2ae40f |
171 | # NOTE: |
172 | # we just alias the original method |
1d68af04 |
173 | # rather than re-produce it here |
ce2ae40f |
174 | 'version' => \&Class::MOP::Module::version |
f0480c45 |
175 | }, |
2e877f58 |
176 | init_arg => undef, |
c4260b45 |
177 | default => sub { \undef } |
f0480c45 |
178 | )) |
179 | ); |
180 | |
181 | # NOTE: |
1d68af04 |
182 | # By following the same conventions as version here, |
183 | # we are opening up the possibility that people can |
184 | # use the $AUTHORITY in non-Class::MOP modules as |
185 | # well. |
f0480c45 |
186 | |
187 | Class::MOP::Module->meta->add_attribute( |
c23184fc |
188 | Class::MOP::Attribute->new('$!authority' => ( |
f0480c45 |
189 | reader => { |
ce2ae40f |
190 | # NOTE: |
191 | # we just alias the original method |
1d68af04 |
192 | # rather than re-produce it here |
ce2ae40f |
193 | 'authority' => \&Class::MOP::Module::authority |
1d68af04 |
194 | }, |
2e877f58 |
195 | init_arg => undef, |
c4260b45 |
196 | default => sub { \undef } |
f0480c45 |
197 | )) |
198 | ); |
199 | |
200 | ## -------------------------------------------------------- |
6d5355c3 |
201 | ## Class::MOP::Class |
202 | |
727919c5 |
203 | Class::MOP::Class->meta->add_attribute( |
c23184fc |
204 | Class::MOP::Attribute->new('%!attributes' => ( |
f7259199 |
205 | reader => { |
1d68af04 |
206 | # NOTE: we need to do this in order |
207 | # for the instance meta-object to |
208 | # not fall into meta-circular death |
209 | # |
ce2ae40f |
210 | # we just alias the original method |
1d68af04 |
211 | # rather than re-produce it here |
ce2ae40f |
212 | 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map |
f7259199 |
213 | }, |
c23184fc |
214 | init_arg => 'attributes', |
727919c5 |
215 | default => sub { {} } |
216 | )) |
217 | ); |
218 | |
351bd7d4 |
219 | Class::MOP::Class->meta->add_attribute( |
c23184fc |
220 | Class::MOP::Attribute->new('%!methods' => ( |
221 | init_arg => 'methods', |
1d68af04 |
222 | reader => { |
ce2ae40f |
223 | # NOTE: |
224 | # we just alias the original method |
1d68af04 |
225 | # rather than re-produce it here |
ce2ae40f |
226 | 'get_method_map' => \&Class::MOP::Class::get_method_map |
92330ee2 |
227 | }, |
7855ddba |
228 | default => sub { {} } |
c4260b45 |
229 | )) |
230 | ); |
231 | |
232 | Class::MOP::Class->meta->add_attribute( |
c23184fc |
233 | Class::MOP::Attribute->new('@!superclasses' => ( |
234 | accessor => { |
235 | # NOTE: |
236 | # we just alias the original method |
1d68af04 |
237 | # rather than re-produce it here |
c23184fc |
238 | 'superclasses' => \&Class::MOP::Class::superclasses |
239 | }, |
2e877f58 |
240 | init_arg => undef, |
c23184fc |
241 | default => sub { \undef } |
242 | )) |
243 | ); |
244 | |
245 | Class::MOP::Class->meta->add_attribute( |
246 | Class::MOP::Attribute->new('$!attribute_metaclass' => ( |
1d68af04 |
247 | reader => { |
6d2118a4 |
248 | # NOTE: |
249 | # we just alias the original method |
1d68af04 |
250 | # rather than re-produce it here |
6d2118a4 |
251 | 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass |
1d68af04 |
252 | }, |
c23184fc |
253 | init_arg => 'attribute_metaclass', |
351bd7d4 |
254 | default => 'Class::MOP::Attribute', |
255 | )) |
256 | ); |
257 | |
258 | Class::MOP::Class->meta->add_attribute( |
c23184fc |
259 | Class::MOP::Attribute->new('$!method_metaclass' => ( |
1d68af04 |
260 | reader => { |
6d2118a4 |
261 | # NOTE: |
262 | # we just alias the original method |
1d68af04 |
263 | # rather than re-produce it here |
6d2118a4 |
264 | 'method_metaclass' => \&Class::MOP::Class::method_metaclass |
265 | }, |
c23184fc |
266 | init_arg => 'method_metaclass', |
1d68af04 |
267 | default => 'Class::MOP::Method', |
351bd7d4 |
268 | )) |
269 | ); |
270 | |
2bab2be6 |
271 | Class::MOP::Class->meta->add_attribute( |
c23184fc |
272 | Class::MOP::Attribute->new('$!instance_metaclass' => ( |
b880e0de |
273 | reader => { |
1d68af04 |
274 | # NOTE: we need to do this in order |
275 | # for the instance meta-object to |
276 | # not fall into meta-circular death |
277 | # |
ce2ae40f |
278 | # we just alias the original method |
1d68af04 |
279 | # rather than re-produce it here |
ce2ae40f |
280 | 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass |
b880e0de |
281 | }, |
c23184fc |
282 | init_arg => 'instance_metaclass', |
1d68af04 |
283 | default => 'Class::MOP::Instance', |
2bab2be6 |
284 | )) |
285 | ); |
286 | |
9d6dce77 |
287 | # NOTE: |
1d68af04 |
288 | # we don't actually need to tie the knot with |
289 | # Class::MOP::Class here, it is actually handled |
290 | # within Class::MOP::Class itself in the |
291 | # construct_class_instance method. |
9d6dce77 |
292 | |
f0480c45 |
293 | ## -------------------------------------------------------- |
727919c5 |
294 | ## Class::MOP::Attribute |
295 | |
7b31baf4 |
296 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
297 | Class::MOP::Attribute->new('$!name' => ( |
298 | init_arg => 'name', |
299 | reader => { |
1d68af04 |
300 | # NOTE: we need to do this in order |
301 | # for the instance meta-object to |
302 | # not fall into meta-circular death |
303 | # |
ce2ae40f |
304 | # we just alias the original method |
1d68af04 |
305 | # rather than re-produce it here |
ce2ae40f |
306 | 'name' => \&Class::MOP::Attribute::name |
b880e0de |
307 | } |
7b31baf4 |
308 | )) |
309 | ); |
310 | |
311 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
312 | Class::MOP::Attribute->new('$!associated_class' => ( |
313 | init_arg => 'associated_class', |
314 | reader => { |
1d68af04 |
315 | # NOTE: we need to do this in order |
316 | # for the instance meta-object to |
317 | # not fall into meta-circular death |
318 | # |
ce2ae40f |
319 | # we just alias the original method |
1d68af04 |
320 | # rather than re-produce it here |
ce2ae40f |
321 | 'associated_class' => \&Class::MOP::Attribute::associated_class |
b880e0de |
322 | } |
7b31baf4 |
323 | )) |
324 | ); |
325 | |
326 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
327 | Class::MOP::Attribute->new('$!accessor' => ( |
328 | init_arg => 'accessor', |
6d2118a4 |
329 | reader => { 'accessor' => \&Class::MOP::Attribute::accessor }, |
330 | predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor }, |
7b31baf4 |
331 | )) |
332 | ); |
333 | |
334 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
335 | Class::MOP::Attribute->new('$!reader' => ( |
336 | init_arg => 'reader', |
6d2118a4 |
337 | reader => { 'reader' => \&Class::MOP::Attribute::reader }, |
338 | predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader }, |
7b31baf4 |
339 | )) |
340 | ); |
341 | |
342 | Class::MOP::Attribute->meta->add_attribute( |
0ab65f99 |
343 | Class::MOP::Attribute->new('$!initializer' => ( |
344 | init_arg => 'initializer', |
8ee74136 |
345 | reader => { 'initializer' => \&Class::MOP::Attribute::initializer }, |
346 | predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer }, |
0ab65f99 |
347 | )) |
348 | ); |
349 | |
350 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
351 | Class::MOP::Attribute->new('$!writer' => ( |
352 | init_arg => 'writer', |
6d2118a4 |
353 | reader => { 'writer' => \&Class::MOP::Attribute::writer }, |
354 | predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer }, |
7b31baf4 |
355 | )) |
356 | ); |
357 | |
358 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
359 | Class::MOP::Attribute->new('$!predicate' => ( |
360 | init_arg => 'predicate', |
6d2118a4 |
361 | reader => { 'predicate' => \&Class::MOP::Attribute::predicate }, |
362 | predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate }, |
7b31baf4 |
363 | )) |
364 | ); |
365 | |
366 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
367 | Class::MOP::Attribute->new('$!clearer' => ( |
368 | init_arg => 'clearer', |
6d2118a4 |
369 | reader => { 'clearer' => \&Class::MOP::Attribute::clearer }, |
370 | predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer }, |
7d28758b |
371 | )) |
372 | ); |
373 | |
374 | Class::MOP::Attribute->meta->add_attribute( |
1d68af04 |
375 | Class::MOP::Attribute->new('$!builder' => ( |
376 | init_arg => 'builder', |
377 | reader => { 'builder' => \&Class::MOP::Attribute::builder }, |
378 | predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder }, |
379 | )) |
380 | ); |
381 | |
382 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
383 | Class::MOP::Attribute->new('$!init_arg' => ( |
384 | init_arg => 'init_arg', |
6d2118a4 |
385 | reader => { 'init_arg' => \&Class::MOP::Attribute::init_arg }, |
386 | predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg }, |
7b31baf4 |
387 | )) |
388 | ); |
389 | |
390 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
391 | Class::MOP::Attribute->new('$!default' => ( |
392 | init_arg => 'default', |
7b31baf4 |
393 | # default has a custom 'reader' method ... |
1d68af04 |
394 | predicate => { 'has_default' => \&Class::MOP::Attribute::has_default }, |
7b31baf4 |
395 | )) |
396 | ); |
397 | |
3545c727 |
398 | Class::MOP::Attribute->meta->add_attribute( |
c23184fc |
399 | Class::MOP::Attribute->new('@!associated_methods' => ( |
400 | init_arg => 'associated_methods', |
401 | reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods }, |
1d68af04 |
402 | default => sub { [] } |
3545c727 |
403 | )) |
404 | ); |
727919c5 |
405 | |
406 | # NOTE: (meta-circularity) |
407 | # This should be one of the last things done |
408 | # it will "tie the knot" with Class::MOP::Attribute |
1d68af04 |
409 | # so that it uses the attributes meta-objects |
410 | # to construct itself. |
727919c5 |
411 | Class::MOP::Attribute->meta->add_method('new' => sub { |
412 | my $class = shift; |
413 | my $name = shift; |
1d68af04 |
414 | my %options = @_; |
415 | |
727919c5 |
416 | (defined $name && $name) |
417 | || confess "You must provide a name for the attribute"; |
1d68af04 |
418 | $options{init_arg} = $name |
5659d76e |
419 | if not exists $options{init_arg}; |
1d68af04 |
420 | |
421 | if(exists $options{builder}){ |
422 | confess("builder must be a defined scalar value which is a method name") |
423 | if ref $options{builder} || !(defined $options{builder}); |
424 | confess("Setting both default and builder is not allowed.") |
425 | if exists $options{default}; |
8fe581e5 |
426 | } else { |
427 | (Class::MOP::Attribute::is_default_a_coderef(\%options)) |
428 | || confess("References are not allowed as default values, you must ". |
429 | "wrap then in a CODE reference (ex: sub { [] } and not [])") |
430 | if exists $options{default} && ref $options{default}; |
1d68af04 |
431 | } |
5659d76e |
432 | # return the new object |
433 | $class->meta->new_object(name => $name, %options); |
434 | }); |
435 | |
436 | Class::MOP::Attribute->meta->add_method('clone' => sub { |
a740253a |
437 | my $self = shift; |
1d68af04 |
438 | $self->meta->clone_object($self, @_); |
727919c5 |
439 | }); |
440 | |
f0480c45 |
441 | ## -------------------------------------------------------- |
b6164407 |
442 | ## Class::MOP::Method |
443 | |
444 | Class::MOP::Method->meta->add_attribute( |
c23184fc |
445 | Class::MOP::Attribute->new('&!body' => ( |
446 | init_arg => 'body', |
447 | reader => { 'body' => \&Class::MOP::Method::body }, |
b6164407 |
448 | )) |
449 | ); |
450 | |
451 | ## -------------------------------------------------------- |
452 | ## Class::MOP::Method::Wrapped |
453 | |
454 | # NOTE: |
1d68af04 |
455 | # the way this item is initialized, this |
456 | # really does not follow the standard |
457 | # practices of attributes, but we put |
b6164407 |
458 | # it here for completeness |
459 | Class::MOP::Method::Wrapped->meta->add_attribute( |
c23184fc |
460 | Class::MOP::Attribute->new('%!modifier_table') |
b6164407 |
461 | ); |
462 | |
463 | ## -------------------------------------------------------- |
565f0cbb |
464 | ## Class::MOP::Method::Generated |
465 | |
466 | Class::MOP::Method::Generated->meta->add_attribute( |
467 | Class::MOP::Attribute->new('$!is_inline' => ( |
468 | init_arg => 'is_inline', |
469 | reader => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline }, |
1d68af04 |
470 | )) |
565f0cbb |
471 | ); |
472 | |
473 | ## -------------------------------------------------------- |
d90b42a6 |
474 | ## Class::MOP::Method::Accessor |
475 | |
476 | Class::MOP::Method::Accessor->meta->add_attribute( |
c23184fc |
477 | Class::MOP::Attribute->new('$!attribute' => ( |
478 | init_arg => 'attribute', |
1d68af04 |
479 | reader => { |
480 | 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute |
d90b42a6 |
481 | }, |
1d68af04 |
482 | )) |
d90b42a6 |
483 | ); |
484 | |
485 | Class::MOP::Method::Accessor->meta->add_attribute( |
c23184fc |
486 | Class::MOP::Attribute->new('$!accessor_type' => ( |
487 | init_arg => 'accessor_type', |
488 | reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type }, |
1d68af04 |
489 | )) |
d90b42a6 |
490 | ); |
491 | |
d90b42a6 |
492 | |
493 | ## -------------------------------------------------------- |
494 | ## Class::MOP::Method::Constructor |
495 | |
496 | Class::MOP::Method::Constructor->meta->add_attribute( |
c23184fc |
497 | Class::MOP::Attribute->new('%!options' => ( |
498 | init_arg => 'options', |
1d68af04 |
499 | reader => { |
500 | 'options' => \&Class::MOP::Method::Constructor::options |
d90b42a6 |
501 | }, |
1d68af04 |
502 | )) |
d90b42a6 |
503 | ); |
504 | |
505 | Class::MOP::Method::Constructor->meta->add_attribute( |
c23184fc |
506 | Class::MOP::Attribute->new('$!associated_metaclass' => ( |
507 | init_arg => 'metaclass', |
1d68af04 |
508 | reader => { |
509 | 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass |
510 | }, |
511 | )) |
d90b42a6 |
512 | ); |
513 | |
514 | ## -------------------------------------------------------- |
86482605 |
515 | ## Class::MOP::Instance |
516 | |
517 | # NOTE: |
1d68af04 |
518 | # these don't yet do much of anything, but are just |
86482605 |
519 | # included for completeness |
520 | |
521 | Class::MOP::Instance->meta->add_attribute( |
c23184fc |
522 | Class::MOP::Attribute->new('$!meta') |
86482605 |
523 | ); |
524 | |
525 | Class::MOP::Instance->meta->add_attribute( |
c23184fc |
526 | Class::MOP::Attribute->new('@!slots') |
86482605 |
527 | ); |
528 | |
529 | ## -------------------------------------------------------- |
f0480c45 |
530 | ## Now close all the Class::MOP::* classes |
4d47b77f |
531 | |
0b9372a2 |
532 | # NOTE: |
1d68af04 |
533 | # we don't need to inline the |
534 | # constructors or the accessors |
535 | # this only lengthens the compile |
536 | # time of the MOP, and gives us |
0b9372a2 |
537 | # no actual benefits. |
538 | |
539 | $_->meta->make_immutable( |
540 | inline_constructor => 0, |
541 | inline_accessors => 0, |
542 | ) for qw/ |
1d68af04 |
543 | Class::MOP::Package |
544 | Class::MOP::Module |
545 | Class::MOP::Class |
546 | |
0b9372a2 |
547 | Class::MOP::Attribute |
1d68af04 |
548 | Class::MOP::Method |
549 | Class::MOP::Instance |
550 | |
551 | Class::MOP::Object |
0b9372a2 |
552 | |
565f0cbb |
553 | Class::MOP::Method::Generated |
1d68af04 |
554 | |
ba38bf08 |
555 | Class::MOP::Method::Accessor |
1d68af04 |
556 | Class::MOP::Method::Constructor |
557 | Class::MOP::Method::Wrapped |
0b9372a2 |
558 | /; |
b6164407 |
559 | |
94b19069 |
560 | 1; |
561 | |
562 | __END__ |
563 | |
564 | =pod |
565 | |
1d68af04 |
566 | =head1 NAME |
94b19069 |
567 | |
568 | Class::MOP - A Meta Object Protocol for Perl 5 |
569 | |
94b19069 |
570 | =head1 DESCRIPTON |
571 | |
127d39a7 |
572 | This module is a fully functioning meta object protocol for the |
1d68af04 |
573 | Perl 5 object system. It makes no attempt to change the behavior or |
574 | characteristics of the Perl 5 object system, only to create a |
27e31eaf |
575 | protocol for its manipulation and introspection. |
94b19069 |
576 | |
1d68af04 |
577 | That said, it does attempt to create the tools for building a rich |
578 | set of extensions to the Perl 5 object system. Every attempt has been |
579 | made for these tools to keep to the spirit of the Perl 5 object |
94b19069 |
580 | system that we all know and love. |
581 | |
1d68af04 |
582 | This documentation is admittedly sparse on details, as time permits |
583 | I will try to improve them. For now, I suggest looking at the items |
584 | listed in the L<SEE ALSO> section for more information. In particular |
585 | the book "The Art of the Meta Object Protocol" was very influential |
40483095 |
586 | in the development of this system. |
587 | |
bfe4d0fc |
588 | =head2 What is a Meta Object Protocol? |
589 | |
1d68af04 |
590 | A meta object protocol is an API to an object system. |
bfe4d0fc |
591 | |
1d68af04 |
592 | To be more specific, it is a set of abstractions of the components of |
593 | an object system (typically things like; classes, object, methods, |
594 | object attributes, etc.). These abstractions can then be used to both |
bfe4d0fc |
595 | inspect and manipulate the object system which they describe. |
596 | |
1d68af04 |
597 | It can be said that there are two MOPs for any object system; the |
598 | implicit MOP, and the explicit MOP. The implicit MOP handles things |
599 | like method dispatch or inheritance, which happen automatically as |
600 | part of how the object system works. The explicit MOP typically |
601 | handles the introspection/reflection features of the object system. |
602 | All object systems have implicit MOPs, without one, they would not |
603 | work. Explict MOPs however as less common, and depending on the |
604 | language can vary from restrictive (Reflection in Java or C#) to |
605 | wide open (CLOS is a perfect example). |
bfe4d0fc |
606 | |
e16da3e6 |
607 | =head2 Yet Another Class Builder!! Why? |
608 | |
1d68af04 |
609 | This is B<not> a class builder so much as it is a I<class builder |
610 | B<builder>>. My intent is that an end user does not use this module |
611 | directly, but instead this module is used by module authors to |
612 | build extensions and features onto the Perl 5 object system. |
e16da3e6 |
613 | |
94b19069 |
614 | =head2 Who is this module for? |
615 | |
1d68af04 |
616 | This module is specifically for anyone who has ever created or |
617 | wanted to create a module for the Class:: namespace. The tools which |
618 | this module will provide will hopefully make it easier to do more |
619 | complex things with Perl 5 classes by removing such barriers as |
620 | the need to hack the symbol tables, or understand the fine details |
621 | of method dispatch. |
94b19069 |
622 | |
bfe4d0fc |
623 | =head2 What changes do I have to make to use this module? |
624 | |
1d68af04 |
625 | This module was designed to be as unintrusive as possible. Many of |
626 | its features are accessible without B<any> change to your existsing |
627 | code at all. It is meant to be a compliment to your existing code and |
628 | not an intrusion on your code base. Unlike many other B<Class::> |
629 | modules, this module B<does not> require you subclass it, or even that |
630 | you C<use> it in within your module's package. |
bfe4d0fc |
631 | |
1d68af04 |
632 | The only features which requires additions to your code are the |
2eb717d5 |
633 | attribute handling and instance construction features, and these are |
1d68af04 |
634 | both completely optional features. The only reason for this is because |
635 | Perl 5's object system does not actually have these features built |
2eb717d5 |
636 | in. More information about this feature can be found below. |
bfe4d0fc |
637 | |
638 | =head2 A Note about Performance? |
639 | |
1d68af04 |
640 | It is a common misconception that explict MOPs are performance drains. |
641 | But this is not a universal truth at all, it is an side-effect of |
642 | specific implementations. For instance, using Java reflection is much |
643 | slower because the JVM cannot take advantage of any compiler |
644 | optimizations, and the JVM has to deal with much more runtime type |
645 | information as well. Reflection in C# is marginally better as it was |
646 | designed into the language and runtime (the CLR). In contrast, CLOS |
647 | (the Common Lisp Object System) was built to support an explicit MOP, |
648 | and so performance is tuned for it. |
649 | |
650 | This library in particular does it's absolute best to avoid putting |
651 | B<any> drain at all upon your code's performance. In fact, by itself |
652 | it does nothing to affect your existing code. So you only pay for |
2eb717d5 |
653 | what you actually use. |
bfe4d0fc |
654 | |
550d56db |
655 | =head2 About Metaclass compatibility |
656 | |
1d68af04 |
657 | This module makes sure that all metaclasses created are both upwards |
658 | and downwards compatible. The topic of metaclass compatibility is |
659 | highly esoteric and is something only encountered when doing deep and |
660 | involved metaclass hacking. There are two basic kinds of metaclass |
661 | incompatibility; upwards and downwards. |
550d56db |
662 | |
1d68af04 |
663 | Upwards metaclass compatibility means that the metaclass of a |
664 | given class is either the same as (or a subclass of) all of the |
550d56db |
665 | class's ancestors. |
666 | |
1d68af04 |
667 | Downward metaclass compatibility means that the metaclasses of a |
668 | given class's anscestors are all either the same as (or a subclass |
550d56db |
669 | of) that metaclass. |
670 | |
1d68af04 |
671 | Here is a diagram showing a set of two classes (C<A> and C<B>) and |
672 | two metaclasses (C<Meta::A> and C<Meta::B>) which have correct |
550d56db |
673 | metaclass compatibility both upwards and downwards. |
674 | |
675 | +---------+ +---------+ |
676 | | Meta::A |<----| Meta::B | <....... (instance of ) |
1d68af04 |
677 | +---------+ +---------+ <------- (inherits from) |
550d56db |
678 | ^ ^ |
679 | : : |
680 | +---------+ +---------+ |
681 | | A |<----| B | |
682 | +---------+ +---------+ |
683 | |
1d68af04 |
684 | As I said this is a highly esoteric topic and one you will only run |
685 | into if you do a lot of subclassing of B<Class::MOP::Class>. If you |
686 | are interested in why this is an issue see the paper |
687 | I<Uniform and safe metaclass composition> linked to in the |
550d56db |
688 | L<SEE ALSO> section of this document. |
689 | |
aa448b16 |
690 | =head2 Using custom metaclasses |
691 | |
1d68af04 |
692 | Always use the metaclass pragma when using a custom metaclass, this |
693 | will ensure the proper initialization order and not accidentely |
694 | create an incorrect type of metaclass for you. This is a very rare |
695 | problem, and one which can only occur if you are doing deep metaclass |
aa448b16 |
696 | programming. So in other words, don't worry about it. |
697 | |
94b19069 |
698 | =head1 PROTOCOLS |
699 | |
127d39a7 |
700 | The protocol is divided into 4 main sub-protocols: |
94b19069 |
701 | |
702 | =over 4 |
703 | |
704 | =item The Class protocol |
705 | |
1d68af04 |
706 | This provides a means of manipulating and introspecting a Perl 5 |
707 | class. It handles all of symbol table hacking for you, and provides |
94b19069 |
708 | a rich set of methods that go beyond simple package introspection. |
709 | |
552e3d24 |
710 | See L<Class::MOP::Class> for more details. |
711 | |
94b19069 |
712 | =item The Attribute protocol |
713 | |
1d68af04 |
714 | This provides a consistent represenation for an attribute of a |
715 | Perl 5 class. Since there are so many ways to create and handle |
127d39a7 |
716 | attributes in Perl 5 OO, this attempts to provide as much of a |
1d68af04 |
717 | unified approach as possible, while giving the freedom and |
94b19069 |
718 | flexibility to subclass for specialization. |
719 | |
552e3d24 |
720 | See L<Class::MOP::Attribute> for more details. |
721 | |
94b19069 |
722 | =item The Method protocol |
723 | |
1d68af04 |
724 | This provides a means of manipulating and introspecting methods in |
725 | the Perl 5 object system. As with attributes, there are many ways to |
726 | approach this topic, so we try to keep it pretty basic, while still |
94b19069 |
727 | making it possible to extend the system in many ways. |
728 | |
552e3d24 |
729 | See L<Class::MOP::Method> for more details. |
94b19069 |
730 | |
127d39a7 |
731 | =item The Instance protocol |
732 | |
733 | This provides a layer of abstraction for creating object instances. |
734 | Since the other layers use this protocol, it is relatively easy to |
735 | change the type of your instances from the default HASH ref to other |
736 | types of references. Several examples are provided in the F<examples/> |
737 | directory included in this distribution. |
738 | |
739 | See L<Class::MOP::Instance> for more details. |
740 | |
94b19069 |
741 | =back |
742 | |
be7677c7 |
743 | =head1 FUNCTIONS |
744 | |
c1d5345a |
745 | =head2 Constants |
746 | |
747 | =over 4 |
748 | |
749 | =item I<IS_RUNNING_ON_5_10> |
750 | |
751 | We set this constant depending on what version perl we are on, this |
752 | allows us to take advantage of new 5.10 features and stay backwards |
753 | compat. |
754 | |
755 | =back |
756 | |
448b6e55 |
757 | =head2 Utility functions |
758 | |
759 | =over 4 |
760 | |
761 | =item B<load_class ($class_name)> |
762 | |
1d68af04 |
763 | This will load a given C<$class_name> and if it does not have an |
448b6e55 |
764 | already initialized metaclass, then it will intialize one for it. |
127d39a7 |
765 | This function can be used in place of tricks like |
766 | C<eval "use $module"> or using C<require>. |
448b6e55 |
767 | |
768 | =item B<is_class_loaded ($class_name)> |
769 | |
1d68af04 |
770 | This will return a boolean depending on if the C<$class_name> has |
771 | been loaded. |
448b6e55 |
772 | |
1d68af04 |
773 | NOTE: This does a basic check of the symbol table to try and |
448b6e55 |
774 | determine as best it can if the C<$class_name> is loaded, it |
1d68af04 |
775 | is probably correct about 99% of the time. |
448b6e55 |
776 | |
b1f5f41d |
777 | =item B<check_package_cache_flag ($pkg)> |
e0e4674a |
778 | |
127d39a7 |
779 | This will return an integer that is managed by C<Class::MOP::Class> |
780 | to determine if a module's symbol table has been altered. |
781 | |
782 | In Perl 5.10 or greater, this flag is package specific. However in |
783 | versions prior to 5.10, this will use the C<PL_sub_generation> variable |
784 | which is not package specific. |
785 | |
e0e4674a |
786 | =item B<get_code_info ($code)> |
787 | |
127d39a7 |
788 | This function returns two values, the name of the package the C<$code> |
789 | is from and the name of the C<$code> itself. This is used by several |
790 | elements of the MOP to detemine where a given C<$code> reference is from. |
791 | |
448b6e55 |
792 | =back |
793 | |
794 | =head2 Metaclass cache functions |
795 | |
1d68af04 |
796 | Class::MOP holds a cache of metaclasses, the following are functions |
797 | (B<not methods>) which can be used to access that cache. It is not |
798 | recommended that you mess with this, bad things could happen. But if |
be7677c7 |
799 | you are brave and willing to risk it, go for it. |
800 | |
801 | =over 4 |
802 | |
803 | =item B<get_all_metaclasses> |
804 | |
1d68af04 |
805 | This will return an hash of all the metaclass instances that have |
806 | been cached by B<Class::MOP::Class> keyed by the package name. |
b9d9fc0b |
807 | |
be7677c7 |
808 | =item B<get_all_metaclass_instances> |
809 | |
1d68af04 |
810 | This will return an array of all the metaclass instances that have |
b9d9fc0b |
811 | been cached by B<Class::MOP::Class>. |
812 | |
be7677c7 |
813 | =item B<get_all_metaclass_names> |
814 | |
1d68af04 |
815 | This will return an array of all the metaclass names that have |
b9d9fc0b |
816 | been cached by B<Class::MOP::Class>. |
817 | |
be7677c7 |
818 | =item B<get_metaclass_by_name ($name)> |
819 | |
127d39a7 |
820 | This will return a cached B<Class::MOP::Class> instance of nothing |
821 | if no metaclass exist by that C<$name>. |
822 | |
be7677c7 |
823 | =item B<store_metaclass_by_name ($name, $meta)> |
824 | |
127d39a7 |
825 | This will store a metaclass in the cache at the supplied C<$key>. |
826 | |
be7677c7 |
827 | =item B<weaken_metaclass ($name)> |
828 | |
127d39a7 |
829 | In rare cases it is desireable to store a weakened reference in |
830 | the metaclass cache. This function will weaken the reference to |
831 | the metaclass stored in C<$name>. |
832 | |
be7677c7 |
833 | =item B<does_metaclass_exist ($name)> |
834 | |
127d39a7 |
835 | This will return true of there exists a metaclass stored in the |
836 | C<$name> key and return false otherwise. |
837 | |
be7677c7 |
838 | =item B<remove_metaclass_by_name ($name)> |
839 | |
127d39a7 |
840 | This will remove a the metaclass stored in the C<$name> key. |
841 | |
be7677c7 |
842 | =back |
843 | |
552e3d24 |
844 | =head1 SEE ALSO |
8b978dd5 |
845 | |
552e3d24 |
846 | =head2 Books |
8b978dd5 |
847 | |
1d68af04 |
848 | There are very few books out on Meta Object Protocols and Metaclasses |
849 | because it is such an esoteric topic. The following books are really |
850 | the only ones I have found. If you know of any more, B<I<please>> |
a2e85e6c |
851 | email me and let me know, I would love to hear about them. |
852 | |
8b978dd5 |
853 | =over 4 |
854 | |
552e3d24 |
855 | =item "The Art of the Meta Object Protocol" |
8b978dd5 |
856 | |
552e3d24 |
857 | =item "Advances in Object-Oriented Metalevel Architecture and Reflection" |
8b978dd5 |
858 | |
b51af7f9 |
859 | =item "Putting MetaClasses to Work" |
860 | |
a2e85e6c |
861 | =item "Smalltalk: The Language" |
862 | |
94b19069 |
863 | =back |
864 | |
550d56db |
865 | =head2 Papers |
866 | |
867 | =over 4 |
868 | |
869 | =item Uniform and safe metaclass composition |
870 | |
1d68af04 |
871 | An excellent paper by the people who brought us the original Traits paper. |
872 | This paper is on how Traits can be used to do safe metaclass composition, |
873 | and offers an excellent introduction section which delves into the topic of |
550d56db |
874 | metaclass compatibility. |
875 | |
876 | L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf> |
877 | |
878 | =item Safe Metaclass Programming |
879 | |
1d68af04 |
880 | This paper seems to precede the above paper, and propose a mix-in based |
881 | approach as opposed to the Traits based approach. Both papers have similar |
882 | information on the metaclass compatibility problem space. |
550d56db |
883 | |
884 | L<http://citeseer.ist.psu.edu/37617.html> |
885 | |
886 | =back |
887 | |
552e3d24 |
888 | =head2 Prior Art |
8b978dd5 |
889 | |
890 | =over 4 |
891 | |
7184ca14 |
892 | =item The Perl 6 MetaModel work in the Pugs project |
8b978dd5 |
893 | |
894 | =over 4 |
895 | |
552e3d24 |
896 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel> |
8b978dd5 |
897 | |
552e3d24 |
898 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace> |
8b978dd5 |
899 | |
900 | =back |
901 | |
94b19069 |
902 | =back |
903 | |
1d68af04 |
904 | =head2 Articles |
f8dfcfb7 |
905 | |
906 | =over 4 |
907 | |
1d68af04 |
908 | =item CPAN Module Review of Class::MOP |
f8dfcfb7 |
909 | |
910 | L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html> |
911 | |
912 | =back |
913 | |
a2e85e6c |
914 | =head1 SIMILAR MODULES |
915 | |
1d68af04 |
916 | As I have said above, this module is a class-builder-builder, so it is |
917 | not the same thing as modules like L<Class::Accessor> and |
918 | L<Class::MethodMaker>. That being said there are very few modules on CPAN |
919 | with similar goals to this module. The one I have found which is most |
920 | like this module is L<Class::Meta>, although it's philosophy and the MOP it |
921 | creates are very different from this modules. |
94b19069 |
922 | |
a2e85e6c |
923 | =head1 BUGS |
924 | |
1d68af04 |
925 | All complex software has bugs lurking in it, and this module is no |
a2e85e6c |
926 | exception. If you find a bug please either email me, or add the bug |
927 | to cpan-RT. |
928 | |
929 | =head1 ACKNOWLEDGEMENTS |
930 | |
931 | =over 4 |
932 | |
b9d9fc0b |
933 | =item Rob Kinyon |
a2e85e6c |
934 | |
1d68af04 |
935 | Thanks to Rob for actually getting the development of this module kick-started. |
a2e85e6c |
936 | |
937 | =back |
938 | |
1a09d9cc |
939 | =head1 AUTHORS |
94b19069 |
940 | |
a2e85e6c |
941 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
552e3d24 |
942 | |
9c8cda90 |
943 | B<with contributions from:> |
944 | |
945 | Brandon (blblack) Black |
946 | |
947 | Guillermo (groditi) Roditi |
948 | |
9195ddff |
949 | Matt (mst) Trout |
950 | |
9c8cda90 |
951 | Rob (robkinyon) Kinyon |
952 | |
953 | Yuval (nothingmuch) Kogman |
1a09d9cc |
954 | |
f430cfa4 |
955 | Scott (konobi) McWhirter |
956 | |
94b19069 |
957 | =head1 COPYRIGHT AND LICENSE |
958 | |
69e3ab0a |
959 | Copyright 2006-2008 by Infinity Interactive, Inc. |
94b19069 |
960 | |
961 | L<http://www.iinteractive.com> |
962 | |
963 | This library is free software; you can redistribute it and/or modify |
1d68af04 |
964 | it under the same terms as Perl itself. |
94b19069 |
965 | |
966 | =cut |