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