Commit | Line | Data |
94b19069 |
1 | |
2 | package Class::MOP; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
727919c5 |
7 | use Carp 'confess'; |
aa448b16 |
8 | use Scalar::Util (); |
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 | |
1a09d9cc |
16 | our $VERSION = '0.31'; |
94b19069 |
17 | |
aa448b16 |
18 | ## ---------------------------------------------------------------------------- |
19 | ## Setting up our environment ... |
20 | ## ---------------------------------------------------------------------------- |
21 | ## Class::MOP needs to have a few things in the global perl environment so |
22 | ## that it can operate effectively. Those things are done here. |
23 | ## ---------------------------------------------------------------------------- |
24 | |
3bf7644b |
25 | # ... nothing yet actually ;) |
8b978dd5 |
26 | |
b51af7f9 |
27 | ## ---------------------------------------------------------------------------- |
28 | ## Bootstrapping |
29 | ## ---------------------------------------------------------------------------- |
30 | ## The code below here is to bootstrap our MOP with itself. This is also |
31 | ## sometimes called "tying the knot". By doing this, we make it much easier |
32 | ## to extend the MOP through subclassing and such since now you can use the |
33 | ## MOP itself to extend itself. |
34 | ## |
35 | ## Yes, I know, thats weird and insane, but it's a good thing, trust me :) |
36 | ## ---------------------------------------------------------------------------- |
727919c5 |
37 | |
38 | # We need to add in the meta-attributes here so that |
39 | # any subclass of Class::MOP::* will be able to |
40 | # inherit them using &construct_instance |
41 | |
6d5355c3 |
42 | ## Class::MOP::Package |
727919c5 |
43 | |
6d5355c3 |
44 | Class::MOP::Package->meta->add_attribute( |
351bd7d4 |
45 | Class::MOP::Attribute->new('$:package' => ( |
b880e0de |
46 | reader => { |
47 | # NOTE: we need to do this in order |
48 | # for the instance meta-object to |
49 | # not fall into meta-circular death |
50 | 'name' => sub { (shift)->{'$:package'} } |
51 | }, |
7b31baf4 |
52 | init_arg => ':package', |
727919c5 |
53 | )) |
54 | ); |
55 | |
9d6dce77 |
56 | # NOTE: |
57 | # use the metaclass to construct the meta-package |
58 | # which is a superclass of the metaclass itself :P |
59 | Class::MOP::Package->meta->add_method('initialize' => sub { |
60 | my $class = shift; |
61 | my $package_name = shift; |
62 | $class->meta->new_object(':package' => $package_name, @_); |
63 | }); |
64 | |
6d5355c3 |
65 | ## Class::MOP::Class |
66 | |
727919c5 |
67 | Class::MOP::Class->meta->add_attribute( |
351bd7d4 |
68 | Class::MOP::Attribute->new('%:attributes' => ( |
f7259199 |
69 | reader => { |
70 | # NOTE: we need to do this in order |
71 | # for the instance meta-object to |
72 | # not fall into meta-circular death |
73 | 'get_attribute_map' => sub { (shift)->{'%:attributes'} } |
74 | }, |
351bd7d4 |
75 | init_arg => ':attributes', |
727919c5 |
76 | default => sub { {} } |
77 | )) |
78 | ); |
79 | |
351bd7d4 |
80 | Class::MOP::Class->meta->add_attribute( |
81 | Class::MOP::Attribute->new('$:attribute_metaclass' => ( |
7b31baf4 |
82 | reader => 'attribute_metaclass', |
351bd7d4 |
83 | init_arg => ':attribute_metaclass', |
84 | default => 'Class::MOP::Attribute', |
85 | )) |
86 | ); |
87 | |
88 | Class::MOP::Class->meta->add_attribute( |
89 | Class::MOP::Attribute->new('$:method_metaclass' => ( |
7b31baf4 |
90 | reader => 'method_metaclass', |
351bd7d4 |
91 | init_arg => ':method_metaclass', |
92 | default => 'Class::MOP::Method', |
93 | )) |
94 | ); |
95 | |
2bab2be6 |
96 | Class::MOP::Class->meta->add_attribute( |
97 | Class::MOP::Attribute->new('$:instance_metaclass' => ( |
b880e0de |
98 | reader => { |
99 | # NOTE: we need to do this in order |
100 | # for the instance meta-object to |
101 | # not fall into meta-circular death |
102 | 'instance_metaclass' => sub { (shift)->{'$:instance_metaclass'} } |
103 | }, |
2bab2be6 |
104 | init_arg => ':instance_metaclass', |
105 | default => 'Class::MOP::Instance', |
106 | )) |
107 | ); |
108 | |
9d6dce77 |
109 | # NOTE: |
110 | # we don't actually need to tie the knot with |
111 | # Class::MOP::Class here, it is actually handled |
112 | # within Class::MOP::Class itself in the |
113 | # construct_class_instance method. |
114 | |
727919c5 |
115 | ## Class::MOP::Attribute |
116 | |
7b31baf4 |
117 | Class::MOP::Attribute->meta->add_attribute( |
118 | Class::MOP::Attribute->new('name' => ( |
b880e0de |
119 | reader => { |
120 | # NOTE: we need to do this in order |
121 | # for the instance meta-object to |
122 | # not fall into meta-circular death |
123 | 'name' => sub { (shift)->{name} } |
124 | } |
7b31baf4 |
125 | )) |
126 | ); |
127 | |
128 | Class::MOP::Attribute->meta->add_attribute( |
129 | Class::MOP::Attribute->new('associated_class' => ( |
b880e0de |
130 | reader => { |
131 | # NOTE: we need to do this in order |
132 | # for the instance meta-object to |
133 | # not fall into meta-circular death |
134 | 'associated_class' => sub { (shift)->{associated_class} } |
135 | } |
7b31baf4 |
136 | )) |
137 | ); |
138 | |
139 | Class::MOP::Attribute->meta->add_attribute( |
140 | Class::MOP::Attribute->new('accessor' => ( |
141 | reader => 'accessor', |
142 | predicate => 'has_accessor', |
143 | )) |
144 | ); |
145 | |
146 | Class::MOP::Attribute->meta->add_attribute( |
147 | Class::MOP::Attribute->new('reader' => ( |
148 | reader => 'reader', |
149 | predicate => 'has_reader', |
150 | )) |
151 | ); |
152 | |
153 | Class::MOP::Attribute->meta->add_attribute( |
154 | Class::MOP::Attribute->new('writer' => ( |
155 | reader => 'writer', |
156 | predicate => 'has_writer', |
157 | )) |
158 | ); |
159 | |
160 | Class::MOP::Attribute->meta->add_attribute( |
161 | Class::MOP::Attribute->new('predicate' => ( |
162 | reader => 'predicate', |
163 | predicate => 'has_predicate', |
164 | )) |
165 | ); |
166 | |
167 | Class::MOP::Attribute->meta->add_attribute( |
168 | Class::MOP::Attribute->new('init_arg' => ( |
169 | reader => 'init_arg', |
170 | predicate => 'has_init_arg', |
171 | )) |
172 | ); |
173 | |
174 | Class::MOP::Attribute->meta->add_attribute( |
175 | Class::MOP::Attribute->new('default' => ( |
176 | # default has a custom 'reader' method ... |
177 | predicate => 'has_default', |
178 | )) |
179 | ); |
180 | |
727919c5 |
181 | |
182 | # NOTE: (meta-circularity) |
183 | # This should be one of the last things done |
184 | # it will "tie the knot" with Class::MOP::Attribute |
185 | # so that it uses the attributes meta-objects |
186 | # to construct itself. |
187 | Class::MOP::Attribute->meta->add_method('new' => sub { |
188 | my $class = shift; |
189 | my $name = shift; |
190 | my %options = @_; |
191 | |
192 | (defined $name && $name) |
193 | || confess "You must provide a name for the attribute"; |
5659d76e |
194 | $options{init_arg} = $name |
195 | if not exists $options{init_arg}; |
651955fb |
196 | |
5659d76e |
197 | # return the new object |
198 | $class->meta->new_object(name => $name, %options); |
199 | }); |
200 | |
201 | Class::MOP::Attribute->meta->add_method('clone' => sub { |
a740253a |
202 | my $self = shift; |
a27ae83f |
203 | $self->meta->clone_object($self, @_); |
727919c5 |
204 | }); |
205 | |
4d47b77f |
206 | ## Try and close Class::MOP::* |
207 | |
208 | Class::MOP::Package ->meta->make_immutable(inline_constructor => 0); |
209 | Class::MOP::Module ->meta->make_immutable(inline_constructor => 0); |
210 | Class::MOP::Class ->meta->make_immutable(inline_constructor => 0); |
211 | Class::MOP::Attribute->meta->make_immutable(inline_constructor => 0); |
212 | Class::MOP::Method ->meta->make_immutable(inline_constructor => 0); |
213 | Class::MOP::Instance ->meta->make_immutable(inline_constructor => 0); |
214 | |
215 | |
94b19069 |
216 | 1; |
217 | |
218 | __END__ |
219 | |
220 | =pod |
221 | |
222 | =head1 NAME |
223 | |
224 | Class::MOP - A Meta Object Protocol for Perl 5 |
225 | |
226 | =head1 SYNOPSIS |
227 | |
a2e85e6c |
228 | # ... This will come later, for now see |
229 | # the other SYNOPSIS for more information |
94b19069 |
230 | |
231 | =head1 DESCRIPTON |
232 | |
233 | This module is an attempt to create a meta object protocol for the |
234 | Perl 5 object system. It makes no attempt to change the behavior or |
235 | characteristics of the Perl 5 object system, only to create a |
27e31eaf |
236 | protocol for its manipulation and introspection. |
94b19069 |
237 | |
238 | That said, it does attempt to create the tools for building a rich |
239 | set of extensions to the Perl 5 object system. Every attempt has been |
240 | made for these tools to keep to the spirit of the Perl 5 object |
241 | system that we all know and love. |
242 | |
40483095 |
243 | This documentation is admittedly sparse on details, as time permits |
244 | I will try to improve them. For now, I suggest looking at the items |
245 | listed in the L<SEE ALSO> section for more information. In particular |
246 | the book "The Art of the Meta Object Protocol" was very influential |
247 | in the development of this system. |
248 | |
bfe4d0fc |
249 | =head2 What is a Meta Object Protocol? |
250 | |
251 | A meta object protocol is an API to an object system. |
252 | |
253 | To be more specific, it is a set of abstractions of the components of |
254 | an object system (typically things like; classes, object, methods, |
255 | object attributes, etc.). These abstractions can then be used to both |
256 | inspect and manipulate the object system which they describe. |
257 | |
258 | It can be said that there are two MOPs for any object system; the |
259 | implicit MOP, and the explicit MOP. The implicit MOP handles things |
260 | like method dispatch or inheritance, which happen automatically as |
261 | part of how the object system works. The explicit MOP typically |
262 | handles the introspection/reflection features of the object system. |
263 | All object systems have implicit MOPs, without one, they would not |
264 | work. Explict MOPs however as less common, and depending on the |
265 | language can vary from restrictive (Reflection in Java or C#) to |
266 | wide open (CLOS is a perfect example). |
267 | |
e16da3e6 |
268 | =head2 Yet Another Class Builder!! Why? |
269 | |
270 | This is B<not> a class builder so much as it is a I<class builder |
271 | B<builder>>. My intent is that an end user does not use this module |
272 | directly, but instead this module is used by module authors to |
273 | build extensions and features onto the Perl 5 object system. |
274 | |
94b19069 |
275 | =head2 Who is this module for? |
276 | |
277 | This module is specifically for anyone who has ever created or |
278 | wanted to create a module for the Class:: namespace. The tools which |
279 | this module will provide will hopefully make it easier to do more |
280 | complex things with Perl 5 classes by removing such barriers as |
281 | the need to hack the symbol tables, or understand the fine details |
282 | of method dispatch. |
283 | |
bfe4d0fc |
284 | =head2 What changes do I have to make to use this module? |
285 | |
2eb717d5 |
286 | This module was designed to be as unintrusive as possible. Many of |
343203ee |
287 | its features are accessible without B<any> change to your existsing |
bfe4d0fc |
288 | code at all. It is meant to be a compliment to your existing code and |
2eb717d5 |
289 | not an intrusion on your code base. Unlike many other B<Class::> |
a2e85e6c |
290 | modules, this module B<does not> require you subclass it, or even that |
291 | you C<use> it in within your module's package. |
bfe4d0fc |
292 | |
2eb717d5 |
293 | The only features which requires additions to your code are the |
294 | attribute handling and instance construction features, and these are |
a2e85e6c |
295 | both completely optional features. The only reason for this is because |
2eb717d5 |
296 | Perl 5's object system does not actually have these features built |
297 | in. More information about this feature can be found below. |
bfe4d0fc |
298 | |
299 | =head2 A Note about Performance? |
300 | |
301 | It is a common misconception that explict MOPs are performance drains. |
302 | But this is not a universal truth at all, it is an side-effect of |
303 | specific implementations. For instance, using Java reflection is much |
304 | slower because the JVM cannot take advantage of any compiler |
305 | optimizations, and the JVM has to deal with much more runtime type |
306 | information as well. Reflection in C# is marginally better as it was |
307 | designed into the language and runtime (the CLR). In contrast, CLOS |
308 | (the Common Lisp Object System) was built to support an explicit MOP, |
309 | and so performance is tuned for it. |
310 | |
311 | This library in particular does it's absolute best to avoid putting |
2eb717d5 |
312 | B<any> drain at all upon your code's performance. In fact, by itself |
313 | it does nothing to affect your existing code. So you only pay for |
314 | what you actually use. |
bfe4d0fc |
315 | |
550d56db |
316 | =head2 About Metaclass compatibility |
317 | |
318 | This module makes sure that all metaclasses created are both upwards |
319 | and downwards compatible. The topic of metaclass compatibility is |
320 | highly esoteric and is something only encountered when doing deep and |
321 | involved metaclass hacking. There are two basic kinds of metaclass |
322 | incompatibility; upwards and downwards. |
323 | |
324 | Upwards metaclass compatibility means that the metaclass of a |
325 | given class is either the same as (or a subclass of) all of the |
326 | class's ancestors. |
327 | |
328 | Downward metaclass compatibility means that the metaclasses of a |
329 | given class's anscestors are all either the same as (or a subclass |
330 | of) that metaclass. |
331 | |
332 | Here is a diagram showing a set of two classes (C<A> and C<B>) and |
333 | two metaclasses (C<Meta::A> and C<Meta::B>) which have correct |
334 | metaclass compatibility both upwards and downwards. |
335 | |
336 | +---------+ +---------+ |
337 | | Meta::A |<----| Meta::B | <....... (instance of ) |
338 | +---------+ +---------+ <------- (inherits from) |
339 | ^ ^ |
340 | : : |
341 | +---------+ +---------+ |
342 | | A |<----| B | |
343 | +---------+ +---------+ |
344 | |
345 | As I said this is a highly esoteric topic and one you will only run |
346 | into if you do a lot of subclassing of B<Class::MOP::Class>. If you |
347 | are interested in why this is an issue see the paper |
348 | I<Uniform and safe metaclass composition> linked to in the |
349 | L<SEE ALSO> section of this document. |
350 | |
aa448b16 |
351 | =head2 Using custom metaclasses |
352 | |
353 | Always use the metaclass pragma when using a custom metaclass, this |
354 | will ensure the proper initialization order and not accidentely |
355 | create an incorrect type of metaclass for you. This is a very rare |
356 | problem, and one which can only occur if you are doing deep metaclass |
357 | programming. So in other words, don't worry about it. |
358 | |
94b19069 |
359 | =head1 PROTOCOLS |
360 | |
361 | The protocol is divided into 3 main sub-protocols: |
362 | |
363 | =over 4 |
364 | |
365 | =item The Class protocol |
366 | |
367 | This provides a means of manipulating and introspecting a Perl 5 |
368 | class. It handles all of symbol table hacking for you, and provides |
369 | a rich set of methods that go beyond simple package introspection. |
370 | |
552e3d24 |
371 | See L<Class::MOP::Class> for more details. |
372 | |
94b19069 |
373 | =item The Attribute protocol |
374 | |
375 | This provides a consistent represenation for an attribute of a |
376 | Perl 5 class. Since there are so many ways to create and handle |
377 | atttributes in Perl 5 OO, this attempts to provide as much of a |
378 | unified approach as possible, while giving the freedom and |
379 | flexibility to subclass for specialization. |
380 | |
552e3d24 |
381 | See L<Class::MOP::Attribute> for more details. |
382 | |
94b19069 |
383 | =item The Method protocol |
384 | |
385 | This provides a means of manipulating and introspecting methods in |
386 | the Perl 5 object system. As with attributes, there are many ways to |
387 | approach this topic, so we try to keep it pretty basic, while still |
388 | making it possible to extend the system in many ways. |
389 | |
552e3d24 |
390 | See L<Class::MOP::Method> for more details. |
94b19069 |
391 | |
392 | =back |
393 | |
552e3d24 |
394 | =head1 SEE ALSO |
8b978dd5 |
395 | |
552e3d24 |
396 | =head2 Books |
8b978dd5 |
397 | |
a2e85e6c |
398 | There are very few books out on Meta Object Protocols and Metaclasses |
399 | because it is such an esoteric topic. The following books are really |
400 | the only ones I have found. If you know of any more, B<I<please>> |
401 | email me and let me know, I would love to hear about them. |
402 | |
8b978dd5 |
403 | =over 4 |
404 | |
552e3d24 |
405 | =item "The Art of the Meta Object Protocol" |
8b978dd5 |
406 | |
552e3d24 |
407 | =item "Advances in Object-Oriented Metalevel Architecture and Reflection" |
8b978dd5 |
408 | |
b51af7f9 |
409 | =item "Putting MetaClasses to Work" |
410 | |
a2e85e6c |
411 | =item "Smalltalk: The Language" |
412 | |
94b19069 |
413 | =back |
414 | |
550d56db |
415 | =head2 Papers |
416 | |
417 | =over 4 |
418 | |
419 | =item Uniform and safe metaclass composition |
420 | |
421 | An excellent paper by the people who brought us the original Traits paper. |
422 | This paper is on how Traits can be used to do safe metaclass composition, |
423 | and offers an excellent introduction section which delves into the topic of |
424 | metaclass compatibility. |
425 | |
426 | L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf> |
427 | |
428 | =item Safe Metaclass Programming |
429 | |
430 | This paper seems to precede the above paper, and propose a mix-in based |
431 | approach as opposed to the Traits based approach. Both papers have similar |
432 | information on the metaclass compatibility problem space. |
433 | |
434 | L<http://citeseer.ist.psu.edu/37617.html> |
435 | |
436 | =back |
437 | |
552e3d24 |
438 | =head2 Prior Art |
8b978dd5 |
439 | |
440 | =over 4 |
441 | |
7184ca14 |
442 | =item The Perl 6 MetaModel work in the Pugs project |
8b978dd5 |
443 | |
444 | =over 4 |
445 | |
552e3d24 |
446 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel> |
8b978dd5 |
447 | |
552e3d24 |
448 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace> |
8b978dd5 |
449 | |
450 | =back |
451 | |
94b19069 |
452 | =back |
453 | |
a2e85e6c |
454 | =head1 SIMILAR MODULES |
455 | |
456 | As I have said above, this module is a class-builder-builder, so it is |
457 | not the same thing as modules like L<Class::Accessor> and |
458 | L<Class::MethodMaker>. That being said there are very few modules on CPAN |
459 | with similar goals to this module. The one I have found which is most |
550d56db |
460 | like this module is L<Class::Meta>, although it's philosophy and the MOP it |
461 | creates are very different from this modules. |
94b19069 |
462 | |
a2e85e6c |
463 | =head1 BUGS |
464 | |
465 | All complex software has bugs lurking in it, and this module is no |
466 | exception. If you find a bug please either email me, or add the bug |
467 | to cpan-RT. |
468 | |
22286063 |
469 | =head1 CODE COVERAGE |
470 | |
471 | I use L<Devel::Cover> to test the code coverage of my tests, below is the |
472 | L<Devel::Cover> report on this module's test suite. |
473 | |
474 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
475 | File stmt bran cond sub pod time total |
476 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
cdfaa4cc |
477 | Class/MOP.pm 100.0 100.0 100.0 100.0 n/a 19.8 100.0 |
478 | Class/MOP/Attribute.pm 100.0 100.0 91.7 61.2 100.0 14.3 87.9 |
479 | Class/MOP/Class.pm 97.6 91.3 77.3 98.4 100.0 56.4 93.2 |
480 | Class/MOP/Instance.pm 91.1 75.0 33.3 91.7 100.0 6.8 90.7 |
481 | Class/MOP/Method.pm 97.6 60.0 52.9 76.9 100.0 1.6 82.6 |
482 | metaclass.pm 100.0 100.0 83.3 100.0 n/a 1.0 97.7 |
22286063 |
483 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
cdfaa4cc |
484 | Total 97.5 88.5 75.5 82.8 100.0 100.0 91.2 |
22286063 |
485 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
486 | |
a2e85e6c |
487 | =head1 ACKNOWLEDGEMENTS |
488 | |
489 | =over 4 |
490 | |
491 | =item Rob Kinyon E<lt>rob@iinteractive.comE<gt> |
492 | |
493 | Thanks to Rob for actually getting the development of this module kick-started. |
494 | |
495 | =back |
496 | |
1a09d9cc |
497 | =head1 AUTHORS |
94b19069 |
498 | |
a2e85e6c |
499 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
552e3d24 |
500 | |
1a09d9cc |
501 | Yuval Kogman E<lt>nothingmuch@woobling.comE<gt> |
502 | |
94b19069 |
503 | =head1 COPYRIGHT AND LICENSE |
504 | |
505 | Copyright 2006 by Infinity Interactive, Inc. |
506 | |
507 | L<http://www.iinteractive.com> |
508 | |
509 | This library is free software; you can redistribute it and/or modify |
510 | it under the same terms as Perl itself. |
511 | |
512 | =cut |