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