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