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