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