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