10 use Class::MOP::Class;
11 use Class::MOP::Attribute;
12 use Class::MOP::Method;
14 our $VERSION = '0.30';
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 ## ----------------------------------------------------------------------------
23 # ... nothing yet actually ;)
25 ## ----------------------------------------------------------------------------
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.
33 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
34 ## ----------------------------------------------------------------------------
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
42 Class::MOP::Class->meta->add_attribute(
43 Class::MOP::Attribute->new('$:package' => (
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'} }
50 init_arg => ':package',
54 Class::MOP::Class->meta->add_attribute(
55 Class::MOP::Attribute->new('%:attributes' => (
57 # NOTE: we need to do this in order
58 # for the instance meta-object to
59 # not fall into meta-circular death
60 'get_attribute_map' => sub { (shift)->{'%:attributes'} }
62 init_arg => ':attributes',
67 Class::MOP::Class->meta->add_attribute(
68 Class::MOP::Attribute->new('$:attribute_metaclass' => (
69 reader => 'attribute_metaclass',
70 init_arg => ':attribute_metaclass',
71 default => 'Class::MOP::Attribute',
75 Class::MOP::Class->meta->add_attribute(
76 Class::MOP::Attribute->new('$:method_metaclass' => (
77 reader => 'method_metaclass',
78 init_arg => ':method_metaclass',
79 default => 'Class::MOP::Method',
83 Class::MOP::Class->meta->add_attribute(
84 Class::MOP::Attribute->new('$:instance_metaclass' => (
86 # NOTE: we need to do this in order
87 # for the instance meta-object to
88 # not fall into meta-circular death
89 'instance_metaclass' => sub { (shift)->{'$:instance_metaclass'} }
91 init_arg => ':instance_metaclass',
92 default => 'Class::MOP::Instance',
96 ## Class::MOP::Attribute
98 Class::MOP::Attribute->meta->add_attribute(
99 Class::MOP::Attribute->new('name' => (
101 # NOTE: we need to do this in order
102 # for the instance meta-object to
103 # not fall into meta-circular death
104 'name' => sub { (shift)->{name} }
109 Class::MOP::Attribute->meta->add_attribute(
110 Class::MOP::Attribute->new('associated_class' => (
112 # NOTE: we need to do this in order
113 # for the instance meta-object to
114 # not fall into meta-circular death
115 'associated_class' => sub { (shift)->{associated_class} }
120 Class::MOP::Attribute->meta->add_attribute(
121 Class::MOP::Attribute->new('accessor' => (
122 reader => 'accessor',
123 predicate => 'has_accessor',
127 Class::MOP::Attribute->meta->add_attribute(
128 Class::MOP::Attribute->new('reader' => (
130 predicate => 'has_reader',
134 Class::MOP::Attribute->meta->add_attribute(
135 Class::MOP::Attribute->new('writer' => (
137 predicate => 'has_writer',
141 Class::MOP::Attribute->meta->add_attribute(
142 Class::MOP::Attribute->new('predicate' => (
143 reader => 'predicate',
144 predicate => 'has_predicate',
148 Class::MOP::Attribute->meta->add_attribute(
149 Class::MOP::Attribute->new('init_arg' => (
150 reader => 'init_arg',
151 predicate => 'has_init_arg',
155 Class::MOP::Attribute->meta->add_attribute(
156 Class::MOP::Attribute->new('default' => (
157 # default has a custom 'reader' method ...
158 predicate => 'has_default',
163 # NOTE: (meta-circularity)
164 # This should be one of the last things done
165 # it will "tie the knot" with Class::MOP::Attribute
166 # so that it uses the attributes meta-objects
167 # to construct itself.
168 Class::MOP::Attribute->meta->add_method('new' => sub {
173 (defined $name && $name)
174 || confess "You must provide a name for the attribute";
175 $options{init_arg} = $name
176 if not exists $options{init_arg};
178 # return the new object
179 $class->meta->new_object(name => $name, %options);
182 Class::MOP::Attribute->meta->add_method('clone' => sub {
184 $self->meta->clone_object($self, @_);
195 Class::MOP - A Meta Object Protocol for Perl 5
199 # ... This will come later, for now see
200 # the other SYNOPSIS for more information
204 This module is an attempt to create a meta object protocol for the
205 Perl 5 object system. It makes no attempt to change the behavior or
206 characteristics of the Perl 5 object system, only to create a
207 protocol for its manipulation and introspection.
209 That said, it does attempt to create the tools for building a rich
210 set of extensions to the Perl 5 object system. Every attempt has been
211 made for these tools to keep to the spirit of the Perl 5 object
212 system that we all know and love.
214 This documentation is admittedly sparse on details, as time permits
215 I will try to improve them. For now, I suggest looking at the items
216 listed in the L<SEE ALSO> section for more information. In particular
217 the book "The Art of the Meta Object Protocol" was very influential
218 in the development of this system.
220 =head2 What is a Meta Object Protocol?
222 A meta object protocol is an API to an object system.
224 To be more specific, it is a set of abstractions of the components of
225 an object system (typically things like; classes, object, methods,
226 object attributes, etc.). These abstractions can then be used to both
227 inspect and manipulate the object system which they describe.
229 It can be said that there are two MOPs for any object system; the
230 implicit MOP, and the explicit MOP. The implicit MOP handles things
231 like method dispatch or inheritance, which happen automatically as
232 part of how the object system works. The explicit MOP typically
233 handles the introspection/reflection features of the object system.
234 All object systems have implicit MOPs, without one, they would not
235 work. Explict MOPs however as less common, and depending on the
236 language can vary from restrictive (Reflection in Java or C#) to
237 wide open (CLOS is a perfect example).
239 =head2 Yet Another Class Builder!! Why?
241 This is B<not> a class builder so much as it is a I<class builder
242 B<builder>>. My intent is that an end user does not use this module
243 directly, but instead this module is used by module authors to
244 build extensions and features onto the Perl 5 object system.
246 =head2 Who is this module for?
248 This module is specifically for anyone who has ever created or
249 wanted to create a module for the Class:: namespace. The tools which
250 this module will provide will hopefully make it easier to do more
251 complex things with Perl 5 classes by removing such barriers as
252 the need to hack the symbol tables, or understand the fine details
255 =head2 What changes do I have to make to use this module?
257 This module was designed to be as unintrusive as possible. Many of
258 its features are accessible without B<any> change to your existsing
259 code at all. It is meant to be a compliment to your existing code and
260 not an intrusion on your code base. Unlike many other B<Class::>
261 modules, this module B<does not> require you subclass it, or even that
262 you C<use> it in within your module's package.
264 The only features which requires additions to your code are the
265 attribute handling and instance construction features, and these are
266 both completely optional features. The only reason for this is because
267 Perl 5's object system does not actually have these features built
268 in. More information about this feature can be found below.
270 =head2 A Note about Performance?
272 It is a common misconception that explict MOPs are performance drains.
273 But this is not a universal truth at all, it is an side-effect of
274 specific implementations. For instance, using Java reflection is much
275 slower because the JVM cannot take advantage of any compiler
276 optimizations, and the JVM has to deal with much more runtime type
277 information as well. Reflection in C# is marginally better as it was
278 designed into the language and runtime (the CLR). In contrast, CLOS
279 (the Common Lisp Object System) was built to support an explicit MOP,
280 and so performance is tuned for it.
282 This library in particular does it's absolute best to avoid putting
283 B<any> drain at all upon your code's performance. In fact, by itself
284 it does nothing to affect your existing code. So you only pay for
285 what you actually use.
287 =head2 About Metaclass compatibility
289 This module makes sure that all metaclasses created are both upwards
290 and downwards compatible. The topic of metaclass compatibility is
291 highly esoteric and is something only encountered when doing deep and
292 involved metaclass hacking. There are two basic kinds of metaclass
293 incompatibility; upwards and downwards.
295 Upwards metaclass compatibility means that the metaclass of a
296 given class is either the same as (or a subclass of) all of the
299 Downward metaclass compatibility means that the metaclasses of a
300 given class's anscestors are all either the same as (or a subclass
303 Here is a diagram showing a set of two classes (C<A> and C<B>) and
304 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
305 metaclass compatibility both upwards and downwards.
307 +---------+ +---------+
308 | Meta::A |<----| Meta::B | <....... (instance of )
309 +---------+ +---------+ <------- (inherits from)
312 +---------+ +---------+
314 +---------+ +---------+
316 As I said this is a highly esoteric topic and one you will only run
317 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
318 are interested in why this is an issue see the paper
319 I<Uniform and safe metaclass composition> linked to in the
320 L<SEE ALSO> section of this document.
322 =head2 Using custom metaclasses
324 Always use the metaclass pragma when using a custom metaclass, this
325 will ensure the proper initialization order and not accidentely
326 create an incorrect type of metaclass for you. This is a very rare
327 problem, and one which can only occur if you are doing deep metaclass
328 programming. So in other words, don't worry about it.
332 The protocol is divided into 3 main sub-protocols:
336 =item The Class protocol
338 This provides a means of manipulating and introspecting a Perl 5
339 class. It handles all of symbol table hacking for you, and provides
340 a rich set of methods that go beyond simple package introspection.
342 See L<Class::MOP::Class> for more details.
344 =item The Attribute protocol
346 This provides a consistent represenation for an attribute of a
347 Perl 5 class. Since there are so many ways to create and handle
348 atttributes in Perl 5 OO, this attempts to provide as much of a
349 unified approach as possible, while giving the freedom and
350 flexibility to subclass for specialization.
352 See L<Class::MOP::Attribute> for more details.
354 =item The Method protocol
356 This provides a means of manipulating and introspecting methods in
357 the Perl 5 object system. As with attributes, there are many ways to
358 approach this topic, so we try to keep it pretty basic, while still
359 making it possible to extend the system in many ways.
361 See L<Class::MOP::Method> for more details.
369 There are very few books out on Meta Object Protocols and Metaclasses
370 because it is such an esoteric topic. The following books are really
371 the only ones I have found. If you know of any more, B<I<please>>
372 email me and let me know, I would love to hear about them.
376 =item "The Art of the Meta Object Protocol"
378 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
380 =item "Putting MetaClasses to Work"
382 =item "Smalltalk: The Language"
390 =item Uniform and safe metaclass composition
392 An excellent paper by the people who brought us the original Traits paper.
393 This paper is on how Traits can be used to do safe metaclass composition,
394 and offers an excellent introduction section which delves into the topic of
395 metaclass compatibility.
397 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
399 =item Safe Metaclass Programming
401 This paper seems to precede the above paper, and propose a mix-in based
402 approach as opposed to the Traits based approach. Both papers have similar
403 information on the metaclass compatibility problem space.
405 L<http://citeseer.ist.psu.edu/37617.html>
413 =item The Perl 6 MetaModel work in the Pugs project
417 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
419 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
425 =head1 SIMILAR MODULES
427 As I have said above, this module is a class-builder-builder, so it is
428 not the same thing as modules like L<Class::Accessor> and
429 L<Class::MethodMaker>. That being said there are very few modules on CPAN
430 with similar goals to this module. The one I have found which is most
431 like this module is L<Class::Meta>, although it's philosophy and the MOP it
432 creates are very different from this modules.
436 All complex software has bugs lurking in it, and this module is no
437 exception. If you find a bug please either email me, or add the bug
442 I use L<Devel::Cover> to test the code coverage of my tests, below is the
443 L<Devel::Cover> report on this module's test suite.
445 ---------------------------- ------ ------ ------ ------ ------ ------ ------
446 File stmt bran cond sub pod time total
447 ---------------------------- ------ ------ ------ ------ ------ ------ ------
448 Class/MOP.pm 100.0 100.0 100.0 100.0 n/a 24.3 100.0
449 Class/MOP/Attribute.pm 100.0 100.0 91.7 63.6 100.0 9.2 88.8
450 Class/MOP/Class.pm 98.1 91.8 77.3 96.8 100.0 58.3 93.3
451 Class/MOP/Instance.pm 87.5 100.0 0.0 87.5 100.0 5.9 88.0
452 Class/MOP/Method.pm 100.0 64.3 52.9 80.0 100.0 1.4 85.3
453 metaclass.pm 100.0 100.0 83.3 100.0 n/a 0.9 97.7
454 ---------------------------- ------ ------ ------ ------ ------ ------ ------
455 Total 97.8 90.1 74.8 82.9 100.0 100.0 91.5
456 ---------------------------- ------ ------ ------ ------ ------ ------ ------
458 =head1 ACKNOWLEDGEMENTS
462 =item Rob Kinyon E<lt>rob@iinteractive.comE<gt>
464 Thanks to Rob for actually getting the development of this module kick-started.
470 Stevan Little E<lt>stevan@iinteractive.comE<gt>
472 =head1 COPYRIGHT AND LICENSE
474 Copyright 2006 by Infinity Interactive, Inc.
476 L<http://www.iinteractive.com>
478 This library is free software; you can redistribute it and/or modify
479 it under the same terms as Perl itself.