10 use Class::MOP::Class;
11 use Class::MOP::Attribute;
12 use Class::MOP::Method;
14 use Class::MOP::Class::Immutable;
16 our $VERSION = '0.31';
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 ## ----------------------------------------------------------------------------
25 # ... nothing yet actually ;)
27 ## ----------------------------------------------------------------------------
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.
35 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
36 ## ----------------------------------------------------------------------------
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
42 ## Class::MOP::Package
44 Class::MOP::Package->meta->add_attribute(
45 Class::MOP::Attribute->new('$:package' => (
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'} }
52 init_arg => ':package',
58 Class::MOP::Class->meta->add_attribute(
59 Class::MOP::Attribute->new('%:attributes' => (
61 # NOTE: we need to do this in order
62 # for the instance meta-object to
63 # not fall into meta-circular death
64 'get_attribute_map' => sub { (shift)->{'%:attributes'} }
66 init_arg => ':attributes',
71 Class::MOP::Class->meta->add_attribute(
72 Class::MOP::Attribute->new('$:attribute_metaclass' => (
73 reader => 'attribute_metaclass',
74 init_arg => ':attribute_metaclass',
75 default => 'Class::MOP::Attribute',
79 Class::MOP::Class->meta->add_attribute(
80 Class::MOP::Attribute->new('$:method_metaclass' => (
81 reader => 'method_metaclass',
82 init_arg => ':method_metaclass',
83 default => 'Class::MOP::Method',
87 Class::MOP::Class->meta->add_attribute(
88 Class::MOP::Attribute->new('$:instance_metaclass' => (
90 # NOTE: we need to do this in order
91 # for the instance meta-object to
92 # not fall into meta-circular death
93 'instance_metaclass' => sub { (shift)->{'$:instance_metaclass'} }
95 init_arg => ':instance_metaclass',
96 default => 'Class::MOP::Instance',
100 ## Class::MOP::Attribute
102 Class::MOP::Attribute->meta->add_attribute(
103 Class::MOP::Attribute->new('name' => (
105 # NOTE: we need to do this in order
106 # for the instance meta-object to
107 # not fall into meta-circular death
108 'name' => sub { (shift)->{name} }
113 Class::MOP::Attribute->meta->add_attribute(
114 Class::MOP::Attribute->new('associated_class' => (
116 # NOTE: we need to do this in order
117 # for the instance meta-object to
118 # not fall into meta-circular death
119 'associated_class' => sub { (shift)->{associated_class} }
124 Class::MOP::Attribute->meta->add_attribute(
125 Class::MOP::Attribute->new('accessor' => (
126 reader => 'accessor',
127 predicate => 'has_accessor',
131 Class::MOP::Attribute->meta->add_attribute(
132 Class::MOP::Attribute->new('reader' => (
134 predicate => 'has_reader',
138 Class::MOP::Attribute->meta->add_attribute(
139 Class::MOP::Attribute->new('writer' => (
141 predicate => 'has_writer',
145 Class::MOP::Attribute->meta->add_attribute(
146 Class::MOP::Attribute->new('predicate' => (
147 reader => 'predicate',
148 predicate => 'has_predicate',
152 Class::MOP::Attribute->meta->add_attribute(
153 Class::MOP::Attribute->new('clearer' => (
155 predicate => 'has_clearer',
159 Class::MOP::Attribute->meta->add_attribute(
160 Class::MOP::Attribute->new('init_arg' => (
161 reader => 'init_arg',
162 predicate => 'has_init_arg',
166 Class::MOP::Attribute->meta->add_attribute(
167 Class::MOP::Attribute->new('default' => (
168 # default has a custom 'reader' method ...
169 predicate => 'has_default',
174 # NOTE: (meta-circularity)
175 # This should be one of the last things done
176 # it will "tie the knot" with Class::MOP::Attribute
177 # so that it uses the attributes meta-objects
178 # to construct itself.
179 Class::MOP::Attribute->meta->add_method('new' => sub {
184 (defined $name && $name)
185 || confess "You must provide a name for the attribute";
186 $options{init_arg} = $name
187 if not exists $options{init_arg};
189 # return the new object
190 $class->meta->new_object(name => $name, %options);
193 Class::MOP::Attribute->meta->add_method('clone' => sub {
195 $self->meta->clone_object($self, @_);
198 ## Try and close Class::MOP::*
200 Class::MOP::Package ->meta->make_immutable(inline_constructor => 0);
201 Class::MOP::Module ->meta->make_immutable(inline_constructor => 0);
202 Class::MOP::Class ->meta->make_immutable(inline_constructor => 0);
203 Class::MOP::Attribute->meta->make_immutable(inline_constructor => 0);
204 Class::MOP::Method ->meta->make_immutable(inline_constructor => 0);
205 Class::MOP::Instance ->meta->make_immutable(inline_constructor => 0);
216 Class::MOP - A Meta Object Protocol for Perl 5
220 # ... This will come later, for now see
221 # the other SYNOPSIS for more information
225 This module is an attempt to create a meta object protocol for the
226 Perl 5 object system. It makes no attempt to change the behavior or
227 characteristics of the Perl 5 object system, only to create a
228 protocol for its manipulation and introspection.
230 That said, it does attempt to create the tools for building a rich
231 set of extensions to the Perl 5 object system. Every attempt has been
232 made for these tools to keep to the spirit of the Perl 5 object
233 system that we all know and love.
235 This documentation is admittedly sparse on details, as time permits
236 I will try to improve them. For now, I suggest looking at the items
237 listed in the L<SEE ALSO> section for more information. In particular
238 the book "The Art of the Meta Object Protocol" was very influential
239 in the development of this system.
241 =head2 What is a Meta Object Protocol?
243 A meta object protocol is an API to an object system.
245 To be more specific, it is a set of abstractions of the components of
246 an object system (typically things like; classes, object, methods,
247 object attributes, etc.). These abstractions can then be used to both
248 inspect and manipulate the object system which they describe.
250 It can be said that there are two MOPs for any object system; the
251 implicit MOP, and the explicit MOP. The implicit MOP handles things
252 like method dispatch or inheritance, which happen automatically as
253 part of how the object system works. The explicit MOP typically
254 handles the introspection/reflection features of the object system.
255 All object systems have implicit MOPs, without one, they would not
256 work. Explict MOPs however as less common, and depending on the
257 language can vary from restrictive (Reflection in Java or C#) to
258 wide open (CLOS is a perfect example).
260 =head2 Yet Another Class Builder!! Why?
262 This is B<not> a class builder so much as it is a I<class builder
263 B<builder>>. My intent is that an end user does not use this module
264 directly, but instead this module is used by module authors to
265 build extensions and features onto the Perl 5 object system.
267 =head2 Who is this module for?
269 This module is specifically for anyone who has ever created or
270 wanted to create a module for the Class:: namespace. The tools which
271 this module will provide will hopefully make it easier to do more
272 complex things with Perl 5 classes by removing such barriers as
273 the need to hack the symbol tables, or understand the fine details
276 =head2 What changes do I have to make to use this module?
278 This module was designed to be as unintrusive as possible. Many of
279 its features are accessible without B<any> change to your existsing
280 code at all. It is meant to be a compliment to your existing code and
281 not an intrusion on your code base. Unlike many other B<Class::>
282 modules, this module B<does not> require you subclass it, or even that
283 you C<use> it in within your module's package.
285 The only features which requires additions to your code are the
286 attribute handling and instance construction features, and these are
287 both completely optional features. The only reason for this is because
288 Perl 5's object system does not actually have these features built
289 in. More information about this feature can be found below.
291 =head2 A Note about Performance?
293 It is a common misconception that explict MOPs are performance drains.
294 But this is not a universal truth at all, it is an side-effect of
295 specific implementations. For instance, using Java reflection is much
296 slower because the JVM cannot take advantage of any compiler
297 optimizations, and the JVM has to deal with much more runtime type
298 information as well. Reflection in C# is marginally better as it was
299 designed into the language and runtime (the CLR). In contrast, CLOS
300 (the Common Lisp Object System) was built to support an explicit MOP,
301 and so performance is tuned for it.
303 This library in particular does it's absolute best to avoid putting
304 B<any> drain at all upon your code's performance. In fact, by itself
305 it does nothing to affect your existing code. So you only pay for
306 what you actually use.
308 =head2 About Metaclass compatibility
310 This module makes sure that all metaclasses created are both upwards
311 and downwards compatible. The topic of metaclass compatibility is
312 highly esoteric and is something only encountered when doing deep and
313 involved metaclass hacking. There are two basic kinds of metaclass
314 incompatibility; upwards and downwards.
316 Upwards metaclass compatibility means that the metaclass of a
317 given class is either the same as (or a subclass of) all of the
320 Downward metaclass compatibility means that the metaclasses of a
321 given class's anscestors are all either the same as (or a subclass
324 Here is a diagram showing a set of two classes (C<A> and C<B>) and
325 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
326 metaclass compatibility both upwards and downwards.
328 +---------+ +---------+
329 | Meta::A |<----| Meta::B | <....... (instance of )
330 +---------+ +---------+ <------- (inherits from)
333 +---------+ +---------+
335 +---------+ +---------+
337 As I said this is a highly esoteric topic and one you will only run
338 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
339 are interested in why this is an issue see the paper
340 I<Uniform and safe metaclass composition> linked to in the
341 L<SEE ALSO> section of this document.
343 =head2 Using custom metaclasses
345 Always use the metaclass pragma when using a custom metaclass, this
346 will ensure the proper initialization order and not accidentely
347 create an incorrect type of metaclass for you. This is a very rare
348 problem, and one which can only occur if you are doing deep metaclass
349 programming. So in other words, don't worry about it.
353 The protocol is divided into 3 main sub-protocols:
357 =item The Class protocol
359 This provides a means of manipulating and introspecting a Perl 5
360 class. It handles all of symbol table hacking for you, and provides
361 a rich set of methods that go beyond simple package introspection.
363 See L<Class::MOP::Class> for more details.
365 =item The Attribute protocol
367 This provides a consistent represenation for an attribute of a
368 Perl 5 class. Since there are so many ways to create and handle
369 atttributes in Perl 5 OO, this attempts to provide as much of a
370 unified approach as possible, while giving the freedom and
371 flexibility to subclass for specialization.
373 See L<Class::MOP::Attribute> for more details.
375 =item The Method protocol
377 This provides a means of manipulating and introspecting methods in
378 the Perl 5 object system. As with attributes, there are many ways to
379 approach this topic, so we try to keep it pretty basic, while still
380 making it possible to extend the system in many ways.
382 See L<Class::MOP::Method> for more details.
390 There are very few books out on Meta Object Protocols and Metaclasses
391 because it is such an esoteric topic. The following books are really
392 the only ones I have found. If you know of any more, B<I<please>>
393 email me and let me know, I would love to hear about them.
397 =item "The Art of the Meta Object Protocol"
399 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
401 =item "Putting MetaClasses to Work"
403 =item "Smalltalk: The Language"
411 =item Uniform and safe metaclass composition
413 An excellent paper by the people who brought us the original Traits paper.
414 This paper is on how Traits can be used to do safe metaclass composition,
415 and offers an excellent introduction section which delves into the topic of
416 metaclass compatibility.
418 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
420 =item Safe Metaclass Programming
422 This paper seems to precede the above paper, and propose a mix-in based
423 approach as opposed to the Traits based approach. Both papers have similar
424 information on the metaclass compatibility problem space.
426 L<http://citeseer.ist.psu.edu/37617.html>
434 =item The Perl 6 MetaModel work in the Pugs project
438 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
440 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
446 =head1 SIMILAR MODULES
448 As I have said above, this module is a class-builder-builder, so it is
449 not the same thing as modules like L<Class::Accessor> and
450 L<Class::MethodMaker>. That being said there are very few modules on CPAN
451 with similar goals to this module. The one I have found which is most
452 like this module is L<Class::Meta>, although it's philosophy and the MOP it
453 creates are very different from this modules.
457 All complex software has bugs lurking in it, and this module is no
458 exception. If you find a bug please either email me, or add the bug
463 I use L<Devel::Cover> to test the code coverage of my tests, below is the
464 L<Devel::Cover> report on this module's test suite.
466 ---------------------------- ------ ------ ------ ------ ------ ------ ------
467 File stmt bran cond sub pod time total
468 ---------------------------- ------ ------ ------ ------ ------ ------ ------
469 Class/MOP.pm 100.0 100.0 100.0 100.0 n/a 19.8 100.0
470 Class/MOP/Attribute.pm 100.0 100.0 91.7 61.2 100.0 14.3 87.9
471 Class/MOP/Class.pm 97.6 91.3 77.3 98.4 100.0 56.4 93.2
472 Class/MOP/Instance.pm 91.1 75.0 33.3 91.7 100.0 6.8 90.7
473 Class/MOP/Method.pm 97.6 60.0 52.9 76.9 100.0 1.6 82.6
474 metaclass.pm 100.0 100.0 83.3 100.0 n/a 1.0 97.7
475 ---------------------------- ------ ------ ------ ------ ------ ------ ------
476 Total 97.5 88.5 75.5 82.8 100.0 100.0 91.2
477 ---------------------------- ------ ------ ------ ------ ------ ------ ------
479 =head1 ACKNOWLEDGEMENTS
483 =item Rob Kinyon E<lt>rob@iinteractive.comE<gt>
485 Thanks to Rob for actually getting the development of this module kick-started.
491 Stevan Little E<lt>stevan@iinteractive.comE<gt>
493 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
495 =head1 COPYRIGHT AND LICENSE
497 Copyright 2006 by Infinity Interactive, Inc.
499 L<http://www.iinteractive.com>
501 This library is free software; you can redistribute it and/or modify
502 it under the same terms as Perl itself.