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