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