Commit | Line | Data |
94b19069 |
1 | |
2 | package Class::MOP; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
727919c5 |
7 | use Carp 'confess'; |
be7677c7 |
8 | use Scalar::Util 'weaken'; |
8b978dd5 |
9 | |
2eb717d5 |
10 | use Class::MOP::Class; |
11 | use Class::MOP::Attribute; |
12 | use Class::MOP::Method; |
13 | |
857f87a7 |
14 | use Class::MOP::Class::Immutable; |
15 | |
b1897d4d |
16 | our $VERSION = '0.35'; |
f0480c45 |
17 | our $AUTHORITY = 'cpan:STEVAN'; |
94b19069 |
18 | |
be7677c7 |
19 | { |
20 | # Metaclasses are singletons, so we cache them here. |
21 | # there is no need to worry about destruction though |
22 | # because they should die only when the program dies. |
23 | # After all, do package definitions even get reaped? |
24 | my %METAS; |
25 | |
26 | # means of accessing all the metaclasses that have |
27 | # been initialized thus far (for mugwumps obj browser) |
28 | sub get_all_metaclasses { %METAS } |
29 | sub get_all_metaclass_instances { values %METAS } |
30 | sub get_all_metaclass_names { keys %METAS } |
31 | sub get_metaclass_by_name { $METAS{$_[0]} } |
32 | sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] } |
33 | sub weaken_metaclass { weaken($METAS{$_[0]}) } |
34 | sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} } |
35 | sub remove_metaclass_by_name { $METAS{$_[0]} = undef } |
36 | |
37 | # NOTE: |
38 | # We only cache metaclasses, meaning instances of |
39 | # Class::MOP::Class. We do not cache instance of |
40 | # Class::MOP::Package or Class::MOP::Module. Mostly |
41 | # because I don't yet see a good reason to do so. |
42 | } |
43 | |
aa448b16 |
44 | ## ---------------------------------------------------------------------------- |
45 | ## Setting up our environment ... |
46 | ## ---------------------------------------------------------------------------- |
47 | ## Class::MOP needs to have a few things in the global perl environment so |
48 | ## that it can operate effectively. Those things are done here. |
49 | ## ---------------------------------------------------------------------------- |
50 | |
3bf7644b |
51 | # ... nothing yet actually ;) |
8b978dd5 |
52 | |
b51af7f9 |
53 | ## ---------------------------------------------------------------------------- |
54 | ## Bootstrapping |
55 | ## ---------------------------------------------------------------------------- |
56 | ## The code below here is to bootstrap our MOP with itself. This is also |
57 | ## sometimes called "tying the knot". By doing this, we make it much easier |
58 | ## to extend the MOP through subclassing and such since now you can use the |
59 | ## MOP itself to extend itself. |
60 | ## |
61 | ## Yes, I know, thats weird and insane, but it's a good thing, trust me :) |
62 | ## ---------------------------------------------------------------------------- |
727919c5 |
63 | |
64 | # We need to add in the meta-attributes here so that |
65 | # any subclass of Class::MOP::* will be able to |
66 | # inherit them using &construct_instance |
67 | |
f0480c45 |
68 | ## -------------------------------------------------------- |
6d5355c3 |
69 | ## Class::MOP::Package |
727919c5 |
70 | |
6d5355c3 |
71 | Class::MOP::Package->meta->add_attribute( |
351bd7d4 |
72 | Class::MOP::Attribute->new('$:package' => ( |
b880e0de |
73 | reader => { |
74 | # NOTE: we need to do this in order |
75 | # for the instance meta-object to |
76 | # not fall into meta-circular death |
ce2ae40f |
77 | # |
78 | # we just alias the original method |
79 | # rather than re-produce it here |
80 | 'name' => \&Class::MOP::Package::name |
b880e0de |
81 | }, |
7b31baf4 |
82 | init_arg => ':package', |
727919c5 |
83 | )) |
84 | ); |
85 | |
a5e51f0b |
86 | Class::MOP::Package->meta->add_attribute( |
87 | Class::MOP::Attribute->new('%:namespace' => ( |
88 | reader => { |
56dcfc1a |
89 | # NOTE: |
ce2ae40f |
90 | # we just alias the original method |
91 | # rather than re-produce it here |
92 | 'namespace' => \&Class::MOP::Package::namespace |
a5e51f0b |
93 | }, |
94 | # NOTE: |
95 | # protect this from silliness |
a2ee6c61 |
96 | init_arg => '!............( DO NOT DO THIS )............!', |
c4260b45 |
97 | default => sub { \undef } |
a5e51f0b |
98 | )) |
99 | ); |
100 | |
9d6dce77 |
101 | # NOTE: |
102 | # use the metaclass to construct the meta-package |
103 | # which is a superclass of the metaclass itself :P |
104 | Class::MOP::Package->meta->add_method('initialize' => sub { |
105 | my $class = shift; |
106 | my $package_name = shift; |
107 | $class->meta->new_object(':package' => $package_name, @_); |
108 | }); |
109 | |
f0480c45 |
110 | ## -------------------------------------------------------- |
111 | ## Class::MOP::Module |
112 | |
113 | # NOTE: |
114 | # yeah this is kind of stretching things a bit, |
115 | # but truthfully the version should be an attribute |
116 | # of the Module, the weirdness comes from having to |
117 | # stick to Perl 5 convention and store it in the |
118 | # $VERSION package variable. Basically if you just |
119 | # squint at it, it will look how you want it to look. |
120 | # Either as a package variable, or as a attribute of |
121 | # the metaclass, isn't abstraction great :) |
122 | |
123 | Class::MOP::Module->meta->add_attribute( |
124 | Class::MOP::Attribute->new('$:version' => ( |
125 | reader => { |
ce2ae40f |
126 | # NOTE: |
127 | # we just alias the original method |
128 | # rather than re-produce it here |
129 | 'version' => \&Class::MOP::Module::version |
f0480c45 |
130 | }, |
131 | # NOTE: |
132 | # protect this from silliness |
133 | init_arg => '!............( DO NOT DO THIS )............!', |
c4260b45 |
134 | default => sub { \undef } |
f0480c45 |
135 | )) |
136 | ); |
137 | |
138 | # NOTE: |
139 | # By following the same conventions as version here, |
140 | # we are opening up the possibility that people can |
141 | # use the $AUTHORITY in non-Class::MOP modules as |
142 | # well. |
143 | |
144 | Class::MOP::Module->meta->add_attribute( |
145 | Class::MOP::Attribute->new('$:authority' => ( |
146 | reader => { |
ce2ae40f |
147 | # NOTE: |
148 | # we just alias the original method |
149 | # rather than re-produce it here |
150 | 'authority' => \&Class::MOP::Module::authority |
f0480c45 |
151 | }, |
152 | # NOTE: |
153 | # protect this from silliness |
154 | init_arg => '!............( DO NOT DO THIS )............!', |
c4260b45 |
155 | default => sub { \undef } |
f0480c45 |
156 | )) |
157 | ); |
158 | |
159 | ## -------------------------------------------------------- |
6d5355c3 |
160 | ## Class::MOP::Class |
161 | |
727919c5 |
162 | Class::MOP::Class->meta->add_attribute( |
351bd7d4 |
163 | Class::MOP::Attribute->new('%:attributes' => ( |
f7259199 |
164 | reader => { |
165 | # NOTE: we need to do this in order |
166 | # for the instance meta-object to |
ce2ae40f |
167 | # not fall into meta-circular death |
168 | # |
169 | # we just alias the original method |
170 | # rather than re-produce it here |
171 | 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map |
f7259199 |
172 | }, |
351bd7d4 |
173 | init_arg => ':attributes', |
727919c5 |
174 | default => sub { {} } |
175 | )) |
176 | ); |
177 | |
351bd7d4 |
178 | Class::MOP::Class->meta->add_attribute( |
c4260b45 |
179 | Class::MOP::Attribute->new('%:methods' => ( |
92330ee2 |
180 | reader => { |
ce2ae40f |
181 | # NOTE: |
182 | # we just alias the original method |
183 | # rather than re-produce it here |
184 | 'get_method_map' => \&Class::MOP::Class::get_method_map |
92330ee2 |
185 | }, |
7855ddba |
186 | default => sub { {} } |
c4260b45 |
187 | )) |
188 | ); |
189 | |
190 | Class::MOP::Class->meta->add_attribute( |
351bd7d4 |
191 | Class::MOP::Attribute->new('$:attribute_metaclass' => ( |
6d2118a4 |
192 | reader => { |
193 | # NOTE: |
194 | # we just alias the original method |
195 | # rather than re-produce it here |
196 | 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass |
197 | }, |
351bd7d4 |
198 | init_arg => ':attribute_metaclass', |
199 | default => 'Class::MOP::Attribute', |
200 | )) |
201 | ); |
202 | |
203 | Class::MOP::Class->meta->add_attribute( |
204 | Class::MOP::Attribute->new('$:method_metaclass' => ( |
6d2118a4 |
205 | reader => { |
206 | # NOTE: |
207 | # we just alias the original method |
208 | # rather than re-produce it here |
209 | 'method_metaclass' => \&Class::MOP::Class::method_metaclass |
210 | }, |
351bd7d4 |
211 | init_arg => ':method_metaclass', |
212 | default => 'Class::MOP::Method', |
213 | )) |
214 | ); |
215 | |
2bab2be6 |
216 | Class::MOP::Class->meta->add_attribute( |
217 | Class::MOP::Attribute->new('$:instance_metaclass' => ( |
b880e0de |
218 | reader => { |
219 | # NOTE: we need to do this in order |
220 | # for the instance meta-object to |
ce2ae40f |
221 | # not fall into meta-circular death |
222 | # |
223 | # we just alias the original method |
224 | # rather than re-produce it here |
225 | 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass |
b880e0de |
226 | }, |
2bab2be6 |
227 | init_arg => ':instance_metaclass', |
228 | default => 'Class::MOP::Instance', |
229 | )) |
230 | ); |
231 | |
9d6dce77 |
232 | # NOTE: |
233 | # we don't actually need to tie the knot with |
234 | # Class::MOP::Class here, it is actually handled |
235 | # within Class::MOP::Class itself in the |
236 | # construct_class_instance method. |
237 | |
f0480c45 |
238 | ## -------------------------------------------------------- |
727919c5 |
239 | ## Class::MOP::Attribute |
240 | |
7b31baf4 |
241 | Class::MOP::Attribute->meta->add_attribute( |
242 | Class::MOP::Attribute->new('name' => ( |
b880e0de |
243 | reader => { |
244 | # NOTE: we need to do this in order |
245 | # for the instance meta-object to |
ce2ae40f |
246 | # not fall into meta-circular death |
247 | # |
248 | # we just alias the original method |
249 | # rather than re-produce it here |
250 | 'name' => \&Class::MOP::Attribute::name |
b880e0de |
251 | } |
7b31baf4 |
252 | )) |
253 | ); |
254 | |
255 | Class::MOP::Attribute->meta->add_attribute( |
256 | Class::MOP::Attribute->new('associated_class' => ( |
b880e0de |
257 | reader => { |
258 | # NOTE: we need to do this in order |
259 | # for the instance meta-object to |
ce2ae40f |
260 | # not fall into meta-circular death |
261 | # |
262 | # we just alias the original method |
263 | # rather than re-produce it here |
264 | 'associated_class' => \&Class::MOP::Attribute::associated_class |
b880e0de |
265 | } |
7b31baf4 |
266 | )) |
267 | ); |
268 | |
269 | Class::MOP::Attribute->meta->add_attribute( |
270 | Class::MOP::Attribute->new('accessor' => ( |
6d2118a4 |
271 | reader => { 'accessor' => \&Class::MOP::Attribute::accessor }, |
272 | predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor }, |
7b31baf4 |
273 | )) |
274 | ); |
275 | |
276 | Class::MOP::Attribute->meta->add_attribute( |
277 | Class::MOP::Attribute->new('reader' => ( |
6d2118a4 |
278 | reader => { 'reader' => \&Class::MOP::Attribute::reader }, |
279 | predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader }, |
7b31baf4 |
280 | )) |
281 | ); |
282 | |
283 | Class::MOP::Attribute->meta->add_attribute( |
284 | Class::MOP::Attribute->new('writer' => ( |
6d2118a4 |
285 | reader => { 'writer' => \&Class::MOP::Attribute::writer }, |
286 | predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer }, |
7b31baf4 |
287 | )) |
288 | ); |
289 | |
290 | Class::MOP::Attribute->meta->add_attribute( |
291 | Class::MOP::Attribute->new('predicate' => ( |
6d2118a4 |
292 | reader => { 'predicate' => \&Class::MOP::Attribute::predicate }, |
293 | predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate }, |
7b31baf4 |
294 | )) |
295 | ); |
296 | |
297 | Class::MOP::Attribute->meta->add_attribute( |
7d28758b |
298 | Class::MOP::Attribute->new('clearer' => ( |
6d2118a4 |
299 | reader => { 'clearer' => \&Class::MOP::Attribute::clearer }, |
300 | predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer }, |
7d28758b |
301 | )) |
302 | ); |
303 | |
304 | Class::MOP::Attribute->meta->add_attribute( |
7b31baf4 |
305 | Class::MOP::Attribute->new('init_arg' => ( |
6d2118a4 |
306 | reader => { 'init_arg' => \&Class::MOP::Attribute::init_arg }, |
307 | predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg }, |
7b31baf4 |
308 | )) |
309 | ); |
310 | |
311 | Class::MOP::Attribute->meta->add_attribute( |
312 | Class::MOP::Attribute->new('default' => ( |
313 | # default has a custom 'reader' method ... |
6d2118a4 |
314 | predicate => { 'has_default' => \&Class::MOP::Attribute::has_default }, |
7b31baf4 |
315 | )) |
316 | ); |
317 | |
3545c727 |
318 | Class::MOP::Attribute->meta->add_attribute( |
319 | Class::MOP::Attribute->new('associated_methods' => ( |
320 | reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods }, |
321 | default => sub { [] } |
322 | )) |
323 | ); |
727919c5 |
324 | |
325 | # NOTE: (meta-circularity) |
326 | # This should be one of the last things done |
327 | # it will "tie the knot" with Class::MOP::Attribute |
328 | # so that it uses the attributes meta-objects |
329 | # to construct itself. |
330 | Class::MOP::Attribute->meta->add_method('new' => sub { |
331 | my $class = shift; |
332 | my $name = shift; |
333 | my %options = @_; |
334 | |
335 | (defined $name && $name) |
336 | || confess "You must provide a name for the attribute"; |
5659d76e |
337 | $options{init_arg} = $name |
338 | if not exists $options{init_arg}; |
148b4697 |
339 | |
340 | (Class::MOP::Attribute::is_default_a_coderef(\%options)) |
341 | || confess("References are not allowed as default values, you must ". |
342 | "wrap then in a CODE reference (ex: sub { [] } and not [])") |
343 | if exists $options{default} && ref $options{default}; |
651955fb |
344 | |
5659d76e |
345 | # return the new object |
346 | $class->meta->new_object(name => $name, %options); |
347 | }); |
348 | |
349 | Class::MOP::Attribute->meta->add_method('clone' => sub { |
a740253a |
350 | my $self = shift; |
a27ae83f |
351 | $self->meta->clone_object($self, @_); |
727919c5 |
352 | }); |
353 | |
f0480c45 |
354 | ## -------------------------------------------------------- |
b6164407 |
355 | ## Class::MOP::Method |
356 | |
357 | Class::MOP::Method->meta->add_attribute( |
358 | Class::MOP::Attribute->new('body' => ( |
6d2118a4 |
359 | reader => { 'body' => \&Class::MOP::Method::body }, |
b6164407 |
360 | )) |
361 | ); |
362 | |
363 | ## -------------------------------------------------------- |
364 | ## Class::MOP::Method::Wrapped |
365 | |
366 | # NOTE: |
367 | # the way this item is initialized, this |
368 | # really does not follow the standard |
369 | # practices of attributes, but we put |
370 | # it here for completeness |
371 | Class::MOP::Method::Wrapped->meta->add_attribute( |
372 | Class::MOP::Attribute->new('modifier_table') |
373 | ); |
374 | |
375 | ## -------------------------------------------------------- |
86482605 |
376 | ## Class::MOP::Instance |
377 | |
378 | # NOTE: |
379 | # these don't yet do much of anything, but are just |
380 | # included for completeness |
381 | |
382 | Class::MOP::Instance->meta->add_attribute( |
383 | Class::MOP::Attribute->new('meta') |
384 | ); |
385 | |
386 | Class::MOP::Instance->meta->add_attribute( |
387 | Class::MOP::Attribute->new('slots') |
388 | ); |
389 | |
390 | ## -------------------------------------------------------- |
f0480c45 |
391 | ## Now close all the Class::MOP::* classes |
4d47b77f |
392 | |
0b9372a2 |
393 | # NOTE: |
394 | # we don't need to inline the |
395 | # constructors or the accessors |
396 | # this only lengthens the compile |
397 | # time of the MOP, and gives us |
398 | # no actual benefits. |
399 | |
400 | $_->meta->make_immutable( |
401 | inline_constructor => 0, |
402 | inline_accessors => 0, |
403 | ) for qw/ |
404 | Class::MOP::Package |
405 | Class::MOP::Module |
406 | Class::MOP::Class |
86482605 |
407 | |
0b9372a2 |
408 | Class::MOP::Attribute |
409 | Class::MOP::Method |
410 | Class::MOP::Instance |
86482605 |
411 | |
0b9372a2 |
412 | Class::MOP::Object |
413 | |
ba38bf08 |
414 | Class::MOP::Method::Accessor |
0b9372a2 |
415 | Class::MOP::Method::Wrapped |
416 | /; |
b6164407 |
417 | |
94b19069 |
418 | 1; |
419 | |
420 | __END__ |
421 | |
422 | =pod |
423 | |
424 | =head1 NAME |
425 | |
426 | Class::MOP - A Meta Object Protocol for Perl 5 |
427 | |
428 | =head1 SYNOPSIS |
429 | |
a2e85e6c |
430 | # ... This will come later, for now see |
431 | # the other SYNOPSIS for more information |
94b19069 |
432 | |
433 | =head1 DESCRIPTON |
434 | |
435 | This module is an attempt to create a meta object protocol for the |
436 | Perl 5 object system. It makes no attempt to change the behavior or |
437 | characteristics of the Perl 5 object system, only to create a |
27e31eaf |
438 | protocol for its manipulation and introspection. |
94b19069 |
439 | |
440 | That said, it does attempt to create the tools for building a rich |
441 | set of extensions to the Perl 5 object system. Every attempt has been |
442 | made for these tools to keep to the spirit of the Perl 5 object |
443 | system that we all know and love. |
444 | |
40483095 |
445 | This documentation is admittedly sparse on details, as time permits |
446 | I will try to improve them. For now, I suggest looking at the items |
447 | listed in the L<SEE ALSO> section for more information. In particular |
448 | the book "The Art of the Meta Object Protocol" was very influential |
449 | in the development of this system. |
450 | |
bfe4d0fc |
451 | =head2 What is a Meta Object Protocol? |
452 | |
453 | A meta object protocol is an API to an object system. |
454 | |
455 | To be more specific, it is a set of abstractions of the components of |
456 | an object system (typically things like; classes, object, methods, |
457 | object attributes, etc.). These abstractions can then be used to both |
458 | inspect and manipulate the object system which they describe. |
459 | |
460 | It can be said that there are two MOPs for any object system; the |
461 | implicit MOP, and the explicit MOP. The implicit MOP handles things |
462 | like method dispatch or inheritance, which happen automatically as |
463 | part of how the object system works. The explicit MOP typically |
464 | handles the introspection/reflection features of the object system. |
465 | All object systems have implicit MOPs, without one, they would not |
466 | work. Explict MOPs however as less common, and depending on the |
467 | language can vary from restrictive (Reflection in Java or C#) to |
468 | wide open (CLOS is a perfect example). |
469 | |
e16da3e6 |
470 | =head2 Yet Another Class Builder!! Why? |
471 | |
472 | This is B<not> a class builder so much as it is a I<class builder |
473 | B<builder>>. My intent is that an end user does not use this module |
474 | directly, but instead this module is used by module authors to |
475 | build extensions and features onto the Perl 5 object system. |
476 | |
94b19069 |
477 | =head2 Who is this module for? |
478 | |
479 | This module is specifically for anyone who has ever created or |
480 | wanted to create a module for the Class:: namespace. The tools which |
481 | this module will provide will hopefully make it easier to do more |
482 | complex things with Perl 5 classes by removing such barriers as |
483 | the need to hack the symbol tables, or understand the fine details |
484 | of method dispatch. |
485 | |
bfe4d0fc |
486 | =head2 What changes do I have to make to use this module? |
487 | |
2eb717d5 |
488 | This module was designed to be as unintrusive as possible. Many of |
343203ee |
489 | its features are accessible without B<any> change to your existsing |
bfe4d0fc |
490 | code at all. It is meant to be a compliment to your existing code and |
2eb717d5 |
491 | not an intrusion on your code base. Unlike many other B<Class::> |
a2e85e6c |
492 | modules, this module B<does not> require you subclass it, or even that |
493 | you C<use> it in within your module's package. |
bfe4d0fc |
494 | |
2eb717d5 |
495 | The only features which requires additions to your code are the |
496 | attribute handling and instance construction features, and these are |
a2e85e6c |
497 | both completely optional features. The only reason for this is because |
2eb717d5 |
498 | Perl 5's object system does not actually have these features built |
499 | in. More information about this feature can be found below. |
bfe4d0fc |
500 | |
501 | =head2 A Note about Performance? |
502 | |
503 | It is a common misconception that explict MOPs are performance drains. |
504 | But this is not a universal truth at all, it is an side-effect of |
505 | specific implementations. For instance, using Java reflection is much |
506 | slower because the JVM cannot take advantage of any compiler |
507 | optimizations, and the JVM has to deal with much more runtime type |
508 | information as well. Reflection in C# is marginally better as it was |
509 | designed into the language and runtime (the CLR). In contrast, CLOS |
510 | (the Common Lisp Object System) was built to support an explicit MOP, |
511 | and so performance is tuned for it. |
512 | |
513 | This library in particular does it's absolute best to avoid putting |
2eb717d5 |
514 | B<any> drain at all upon your code's performance. In fact, by itself |
515 | it does nothing to affect your existing code. So you only pay for |
516 | what you actually use. |
bfe4d0fc |
517 | |
550d56db |
518 | =head2 About Metaclass compatibility |
519 | |
520 | This module makes sure that all metaclasses created are both upwards |
521 | and downwards compatible. The topic of metaclass compatibility is |
522 | highly esoteric and is something only encountered when doing deep and |
523 | involved metaclass hacking. There are two basic kinds of metaclass |
524 | incompatibility; upwards and downwards. |
525 | |
526 | Upwards metaclass compatibility means that the metaclass of a |
527 | given class is either the same as (or a subclass of) all of the |
528 | class's ancestors. |
529 | |
530 | Downward metaclass compatibility means that the metaclasses of a |
531 | given class's anscestors are all either the same as (or a subclass |
532 | of) that metaclass. |
533 | |
534 | Here is a diagram showing a set of two classes (C<A> and C<B>) and |
535 | two metaclasses (C<Meta::A> and C<Meta::B>) which have correct |
536 | metaclass compatibility both upwards and downwards. |
537 | |
538 | +---------+ +---------+ |
539 | | Meta::A |<----| Meta::B | <....... (instance of ) |
540 | +---------+ +---------+ <------- (inherits from) |
541 | ^ ^ |
542 | : : |
543 | +---------+ +---------+ |
544 | | A |<----| B | |
545 | +---------+ +---------+ |
546 | |
547 | As I said this is a highly esoteric topic and one you will only run |
548 | into if you do a lot of subclassing of B<Class::MOP::Class>. If you |
549 | are interested in why this is an issue see the paper |
550 | I<Uniform and safe metaclass composition> linked to in the |
551 | L<SEE ALSO> section of this document. |
552 | |
aa448b16 |
553 | =head2 Using custom metaclasses |
554 | |
555 | Always use the metaclass pragma when using a custom metaclass, this |
556 | will ensure the proper initialization order and not accidentely |
557 | create an incorrect type of metaclass for you. This is a very rare |
558 | problem, and one which can only occur if you are doing deep metaclass |
559 | programming. So in other words, don't worry about it. |
560 | |
94b19069 |
561 | =head1 PROTOCOLS |
562 | |
563 | The protocol is divided into 3 main sub-protocols: |
564 | |
565 | =over 4 |
566 | |
567 | =item The Class protocol |
568 | |
569 | This provides a means of manipulating and introspecting a Perl 5 |
570 | class. It handles all of symbol table hacking for you, and provides |
571 | a rich set of methods that go beyond simple package introspection. |
572 | |
552e3d24 |
573 | See L<Class::MOP::Class> for more details. |
574 | |
94b19069 |
575 | =item The Attribute protocol |
576 | |
577 | This provides a consistent represenation for an attribute of a |
578 | Perl 5 class. Since there are so many ways to create and handle |
579 | atttributes in Perl 5 OO, this attempts to provide as much of a |
580 | unified approach as possible, while giving the freedom and |
581 | flexibility to subclass for specialization. |
582 | |
552e3d24 |
583 | See L<Class::MOP::Attribute> for more details. |
584 | |
94b19069 |
585 | =item The Method protocol |
586 | |
587 | This provides a means of manipulating and introspecting methods in |
588 | the Perl 5 object system. As with attributes, there are many ways to |
589 | approach this topic, so we try to keep it pretty basic, while still |
590 | making it possible to extend the system in many ways. |
591 | |
552e3d24 |
592 | See L<Class::MOP::Method> for more details. |
94b19069 |
593 | |
594 | =back |
595 | |
be7677c7 |
596 | =head1 FUNCTIONS |
597 | |
598 | Class::MOP holds a cache of metaclasses, the following are functions |
599 | (B<not methods>) which can be used to access that cache. It is not |
600 | recommended that you mess with this, bad things could happen. But if |
601 | you are brave and willing to risk it, go for it. |
602 | |
603 | =over 4 |
604 | |
605 | =item B<get_all_metaclasses> |
606 | |
b9d9fc0b |
607 | This will return an hash of all the metaclass instances that have |
608 | been cached by B<Class::MOP::Class> keyed by the package name. |
609 | |
be7677c7 |
610 | =item B<get_all_metaclass_instances> |
611 | |
b9d9fc0b |
612 | This will return an array of all the metaclass instances that have |
613 | been cached by B<Class::MOP::Class>. |
614 | |
be7677c7 |
615 | =item B<get_all_metaclass_names> |
616 | |
b9d9fc0b |
617 | This will return an array of all the metaclass names that have |
618 | been cached by B<Class::MOP::Class>. |
619 | |
be7677c7 |
620 | =item B<get_metaclass_by_name ($name)> |
621 | |
622 | =item B<store_metaclass_by_name ($name, $meta)> |
623 | |
624 | =item B<weaken_metaclass ($name)> |
625 | |
626 | =item B<does_metaclass_exist ($name)> |
627 | |
628 | =item B<remove_metaclass_by_name ($name)> |
629 | |
630 | =back |
631 | |
552e3d24 |
632 | =head1 SEE ALSO |
8b978dd5 |
633 | |
552e3d24 |
634 | =head2 Books |
8b978dd5 |
635 | |
a2e85e6c |
636 | There are very few books out on Meta Object Protocols and Metaclasses |
637 | because it is such an esoteric topic. The following books are really |
638 | the only ones I have found. If you know of any more, B<I<please>> |
639 | email me and let me know, I would love to hear about them. |
640 | |
8b978dd5 |
641 | =over 4 |
642 | |
552e3d24 |
643 | =item "The Art of the Meta Object Protocol" |
8b978dd5 |
644 | |
552e3d24 |
645 | =item "Advances in Object-Oriented Metalevel Architecture and Reflection" |
8b978dd5 |
646 | |
b51af7f9 |
647 | =item "Putting MetaClasses to Work" |
648 | |
a2e85e6c |
649 | =item "Smalltalk: The Language" |
650 | |
94b19069 |
651 | =back |
652 | |
550d56db |
653 | =head2 Papers |
654 | |
655 | =over 4 |
656 | |
657 | =item Uniform and safe metaclass composition |
658 | |
659 | An excellent paper by the people who brought us the original Traits paper. |
660 | This paper is on how Traits can be used to do safe metaclass composition, |
661 | and offers an excellent introduction section which delves into the topic of |
662 | metaclass compatibility. |
663 | |
664 | L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf> |
665 | |
666 | =item Safe Metaclass Programming |
667 | |
668 | This paper seems to precede the above paper, and propose a mix-in based |
669 | approach as opposed to the Traits based approach. Both papers have similar |
670 | information on the metaclass compatibility problem space. |
671 | |
672 | L<http://citeseer.ist.psu.edu/37617.html> |
673 | |
674 | =back |
675 | |
552e3d24 |
676 | =head2 Prior Art |
8b978dd5 |
677 | |
678 | =over 4 |
679 | |
7184ca14 |
680 | =item The Perl 6 MetaModel work in the Pugs project |
8b978dd5 |
681 | |
682 | =over 4 |
683 | |
552e3d24 |
684 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel> |
8b978dd5 |
685 | |
552e3d24 |
686 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace> |
8b978dd5 |
687 | |
688 | =back |
689 | |
94b19069 |
690 | =back |
691 | |
f8dfcfb7 |
692 | =head2 Article |
693 | |
694 | =over 4 |
695 | |
696 | =item CPAN Module Review of Class::MOP |
697 | |
698 | L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html> |
699 | |
700 | =back |
701 | |
a2e85e6c |
702 | =head1 SIMILAR MODULES |
703 | |
704 | As I have said above, this module is a class-builder-builder, so it is |
705 | not the same thing as modules like L<Class::Accessor> and |
706 | L<Class::MethodMaker>. That being said there are very few modules on CPAN |
707 | with similar goals to this module. The one I have found which is most |
550d56db |
708 | like this module is L<Class::Meta>, although it's philosophy and the MOP it |
709 | creates are very different from this modules. |
94b19069 |
710 | |
a2e85e6c |
711 | =head1 BUGS |
712 | |
713 | All complex software has bugs lurking in it, and this module is no |
714 | exception. If you find a bug please either email me, or add the bug |
715 | to cpan-RT. |
716 | |
22286063 |
717 | =head1 CODE COVERAGE |
718 | |
719 | I use L<Devel::Cover> to test the code coverage of my tests, below is the |
720 | L<Devel::Cover> report on this module's test suite. |
721 | |
722 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
723 | File stmt bran cond sub pod time total |
724 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
cf3fa89c |
725 | Class/MOP.pm 97.7 100.0 88.9 94.7 100.0 3.2 96.6 |
726 | Class/MOP/Attribute.pm 75.5 77.9 82.4 88.3 100.0 4.0 81.5 |
727 | Class/MOP/Class.pm 96.9 88.8 72.1 98.2 100.0 35.8 91.4 |
728 | Class/MOP/Class/Immutable.pm 88.2 60.0 n/a 95.5 100.0 0.5 84.6 |
729 | Class/MOP/Instance.pm 86.4 75.0 33.3 86.2 100.0 1.2 87.5 |
730 | Class/MOP/Method.pm 97.5 75.0 61.5 80.6 100.0 12.7 89.7 |
731 | Class/MOP/Module.pm 100.0 n/a 55.6 100.0 100.0 0.1 90.7 |
732 | Class/MOP/Object.pm 73.3 n/a 20.0 80.0 100.0 0.1 66.7 |
733 | Class/MOP/Package.pm 94.6 71.7 33.3 100.0 100.0 42.2 87.0 |
734 | metaclass.pm 100.0 100.0 83.3 100.0 n/a 0.2 97.7 |
22286063 |
735 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
cf3fa89c |
736 | Total 91.3 80.4 69.8 91.9 100.0 100.0 88.1 |
22286063 |
737 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
738 | |
a2e85e6c |
739 | =head1 ACKNOWLEDGEMENTS |
740 | |
741 | =over 4 |
742 | |
b9d9fc0b |
743 | =item Rob Kinyon |
a2e85e6c |
744 | |
745 | Thanks to Rob for actually getting the development of this module kick-started. |
746 | |
747 | =back |
748 | |
1a09d9cc |
749 | =head1 AUTHORS |
94b19069 |
750 | |
a2e85e6c |
751 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
552e3d24 |
752 | |
1a09d9cc |
753 | Yuval Kogman E<lt>nothingmuch@woobling.comE<gt> |
754 | |
94b19069 |
755 | =head1 COPYRIGHT AND LICENSE |
756 | |
757 | Copyright 2006 by Infinity Interactive, Inc. |
758 | |
759 | L<http://www.iinteractive.com> |
760 | |
761 | This library is free software; you can redistribute it and/or modify |
762 | it under the same terms as Perl itself. |
763 | |
764 | =cut |