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