10 use Class::MOP::Class;
11 use Class::MOP::Attribute;
12 use Class::MOP::Method;
14 our $VERSION = '0.07';
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 # so that mixins can have runtime
24 # dispatched SUPER calls
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
44 Class::MOP::Class->meta->add_attribute(
45 Class::MOP::Attribute->new('$:package' => (
47 init_arg => ':package',
51 Class::MOP::Class->meta->add_attribute(
52 Class::MOP::Attribute->new('%:attributes' => (
53 reader => 'get_attribute_map',
54 init_arg => ':attributes',
59 Class::MOP::Class->meta->add_attribute(
60 Class::MOP::Attribute->new('$:attribute_metaclass' => (
61 reader => 'attribute_metaclass',
62 init_arg => ':attribute_metaclass',
63 default => 'Class::MOP::Attribute',
67 Class::MOP::Class->meta->add_attribute(
68 Class::MOP::Attribute->new('$:method_metaclass' => (
69 reader => 'method_metaclass',
70 init_arg => ':method_metaclass',
71 default => 'Class::MOP::Method',
75 ## Class::MOP::Attribute
77 Class::MOP::Attribute->meta->add_attribute(
78 Class::MOP::Attribute->new('name' => (
83 Class::MOP::Attribute->meta->add_attribute(
84 Class::MOP::Attribute->new('associated_class' => (
85 reader => 'associated_class'
89 Class::MOP::Attribute->meta->add_attribute(
90 Class::MOP::Attribute->new('accessor' => (
92 predicate => 'has_accessor',
96 Class::MOP::Attribute->meta->add_attribute(
97 Class::MOP::Attribute->new('reader' => (
99 predicate => 'has_reader',
103 Class::MOP::Attribute->meta->add_attribute(
104 Class::MOP::Attribute->new('writer' => (
106 predicate => 'has_writer',
110 Class::MOP::Attribute->meta->add_attribute(
111 Class::MOP::Attribute->new('predicate' => (
112 reader => 'predicate',
113 predicate => 'has_predicate',
117 Class::MOP::Attribute->meta->add_attribute(
118 Class::MOP::Attribute->new('init_arg' => (
119 reader => 'init_arg',
120 predicate => 'has_init_arg',
124 Class::MOP::Attribute->meta->add_attribute(
125 Class::MOP::Attribute->new('default' => (
126 # default has a custom 'reader' method ...
127 predicate => 'has_default',
132 # NOTE: (meta-circularity)
133 # This should be one of the last things done
134 # it will "tie the knot" with Class::MOP::Attribute
135 # so that it uses the attributes meta-objects
136 # to construct itself.
137 Class::MOP::Attribute->meta->add_method('new' => sub {
142 (defined $name && $name)
143 || confess "You must provide a name for the attribute";
144 $options{init_arg} = $name
145 if not exists $options{init_arg};
147 # return the new object
148 $class->meta->new_object(name => $name, %options);
151 Class::MOP::Attribute->meta->add_method('clone' => sub {
153 my $class = $self->associated_class;
154 $self->detach_from_class() if defined $class;
155 my $clone = $self->meta->clone_object($self, @_);
156 if (defined $class) {
157 $self->attach_to_class($class);
158 $clone->attach_to_class($class);
171 Class::MOP - A Meta Object Protocol for Perl 5
175 # ... This will come later, for now see
176 # the other SYNOPSIS for more information
180 This module is an attempt to create a meta object protocol for the
181 Perl 5 object system. It makes no attempt to change the behavior or
182 characteristics of the Perl 5 object system, only to create a
183 protocol for its manipulation and introspection.
185 That said, it does attempt to create the tools for building a rich
186 set of extensions to the Perl 5 object system. Every attempt has been
187 made for these tools to keep to the spirit of the Perl 5 object
188 system that we all know and love.
190 =head2 What is a Meta Object Protocol?
192 A meta object protocol is an API to an object system.
194 To be more specific, it is a set of abstractions of the components of
195 an object system (typically things like; classes, object, methods,
196 object attributes, etc.). These abstractions can then be used to both
197 inspect and manipulate the object system which they describe.
199 It can be said that there are two MOPs for any object system; the
200 implicit MOP, and the explicit MOP. The implicit MOP handles things
201 like method dispatch or inheritance, which happen automatically as
202 part of how the object system works. The explicit MOP typically
203 handles the introspection/reflection features of the object system.
204 All object systems have implicit MOPs, without one, they would not
205 work. Explict MOPs however as less common, and depending on the
206 language can vary from restrictive (Reflection in Java or C#) to
207 wide open (CLOS is a perfect example).
209 =head2 Yet Another Class Builder!! Why?
211 This is B<not> a class builder so much as it is a I<class builder
212 B<builder>>. My intent is that an end user does not use this module
213 directly, but instead this module is used by module authors to
214 build extensions and features onto the Perl 5 object system.
216 =head2 Who is this module for?
218 This module is specifically for anyone who has ever created or
219 wanted to create a module for the Class:: namespace. The tools which
220 this module will provide will hopefully make it easier to do more
221 complex things with Perl 5 classes by removing such barriers as
222 the need to hack the symbol tables, or understand the fine details
225 =head2 What changes do I have to make to use this module?
227 This module was designed to be as unintrusive as possible. Many of
228 its features are accessible without B<any> change to your existsing
229 code at all. It is meant to be a compliment to your existing code and
230 not an intrusion on your code base. Unlike many other B<Class::>
231 modules, this module B<does not> require you subclass it, or even that
232 you C<use> it in within your module's package.
234 The only features which requires additions to your code are the
235 attribute handling and instance construction features, and these are
236 both completely optional features. The only reason for this is because
237 Perl 5's object system does not actually have these features built
238 in. More information about this feature can be found below.
240 =head2 A Note about Performance?
242 It is a common misconception that explict MOPs are performance drains.
243 But this is not a universal truth at all, it is an side-effect of
244 specific implementations. For instance, using Java reflection is much
245 slower because the JVM cannot take advantage of any compiler
246 optimizations, and the JVM has to deal with much more runtime type
247 information as well. Reflection in C# is marginally better as it was
248 designed into the language and runtime (the CLR). In contrast, CLOS
249 (the Common Lisp Object System) was built to support an explicit MOP,
250 and so performance is tuned for it.
252 This library in particular does it's absolute best to avoid putting
253 B<any> drain at all upon your code's performance. In fact, by itself
254 it does nothing to affect your existing code. So you only pay for
255 what you actually use.
257 =head2 About Metaclass compatibility
259 This module makes sure that all metaclasses created are both upwards
260 and downwards compatible. The topic of metaclass compatibility is
261 highly esoteric and is something only encountered when doing deep and
262 involved metaclass hacking. There are two basic kinds of metaclass
263 incompatibility; upwards and downwards.
265 Upwards metaclass compatibility means that the metaclass of a
266 given class is either the same as (or a subclass of) all of the
269 Downward metaclass compatibility means that the metaclasses of a
270 given class's anscestors are all either the same as (or a subclass
273 Here is a diagram showing a set of two classes (C<A> and C<B>) and
274 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
275 metaclass compatibility both upwards and downwards.
277 +---------+ +---------+
278 | Meta::A |<----| Meta::B | <....... (instance of )
279 +---------+ +---------+ <------- (inherits from)
282 +---------+ +---------+
284 +---------+ +---------+
286 As I said this is a highly esoteric topic and one you will only run
287 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
288 are interested in why this is an issue see the paper
289 I<Uniform and safe metaclass composition> linked to in the
290 L<SEE ALSO> section of this document.
292 =head2 Using custom metaclasses
294 Always use the metaclass pragma when using a custom metaclass, this
295 will ensure the proper initialization order and not accidentely
296 create an incorrect type of metaclass for you. This is a very rare
297 problem, and one which can only occur if you are doing deep metaclass
298 programming. So in other words, don't worry about it.
302 The protocol is divided into 3 main sub-protocols:
306 =item The Class protocol
308 This provides a means of manipulating and introspecting a Perl 5
309 class. It handles all of symbol table hacking for you, and provides
310 a rich set of methods that go beyond simple package introspection.
312 See L<Class::MOP::Class> for more details.
314 =item The Attribute protocol
316 This provides a consistent represenation for an attribute of a
317 Perl 5 class. Since there are so many ways to create and handle
318 atttributes in Perl 5 OO, this attempts to provide as much of a
319 unified approach as possible, while giving the freedom and
320 flexibility to subclass for specialization.
322 See L<Class::MOP::Attribute> for more details.
324 =item The Method protocol
326 This provides a means of manipulating and introspecting methods in
327 the Perl 5 object system. As with attributes, there are many ways to
328 approach this topic, so we try to keep it pretty basic, while still
329 making it possible to extend the system in many ways.
331 See L<Class::MOP::Method> for more details.
339 There are very few books out on Meta Object Protocols and Metaclasses
340 because it is such an esoteric topic. The following books are really
341 the only ones I have found. If you know of any more, B<I<please>>
342 email me and let me know, I would love to hear about them.
346 =item "The Art of the Meta Object Protocol"
348 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
350 =item "Putting MetaClasses to Work"
352 =item "Smalltalk: The Language"
360 =item Uniform and safe metaclass composition
362 An excellent paper by the people who brought us the original Traits paper.
363 This paper is on how Traits can be used to do safe metaclass composition,
364 and offers an excellent introduction section which delves into the topic of
365 metaclass compatibility.
367 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
369 =item Safe Metaclass Programming
371 This paper seems to precede the above paper, and propose a mix-in based
372 approach as opposed to the Traits based approach. Both papers have similar
373 information on the metaclass compatibility problem space.
375 L<http://citeseer.ist.psu.edu/37617.html>
383 =item The Perl 6 MetaModel work in the Pugs project
387 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
389 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
395 =head1 SIMILAR MODULES
397 As I have said above, this module is a class-builder-builder, so it is
398 not the same thing as modules like L<Class::Accessor> and
399 L<Class::MethodMaker>. That being said there are very few modules on CPAN
400 with similar goals to this module. The one I have found which is most
401 like this module is L<Class::Meta>, although it's philosophy and the MOP it
402 creates are very different from this modules.
406 All complex software has bugs lurking in it, and this module is no
407 exception. If you find a bug please either email me, or add the bug
412 I use L<Devel::Cover> to test the code coverage of my tests, below is the
413 L<Devel::Cover> report on this module's test suite.
415 ---------------------------- ------ ------ ------ ------ ------ ------ ------
416 File stmt bran cond sub pod time total
417 ---------------------------- ------ ------ ------ ------ ------ ------ ------
418 Class/MOP.pm 100.0 100.0 100.0 100.0 n/a 21.4 100.0
419 Class/MOP/Attribute.pm 100.0 100.0 88.9 100.0 100.0 27.1 99.3
420 Class/MOP/Class.pm 100.0 100.0 93.7 100.0 100.0 44.8 99.1
421 Class/MOP/Method.pm 100.0 100.0 83.3 100.0 100.0 4.8 97.1
422 metaclass.pm 100.0 100.0 80.0 100.0 n/a 1.9 97.3
423 ---------------------------- ------ ------ ------ ------ ------ ------ ------
424 Total 100.0 100.0 92.2 100.0 100.0 100.0 99.0
425 ---------------------------- ------ ------ ------ ------ ------ ------ ------
427 =head1 ACKNOWLEDGEMENTS
431 =item Rob Kinyon E<lt>rob@iinteractive.comE<gt>
433 Thanks to Rob for actually getting the development of this module kick-started.
439 Stevan Little E<lt>stevan@iinteractive.comE<gt>
441 =head1 COPYRIGHT AND LICENSE
443 Copyright 2006 by Infinity Interactive, Inc.
445 L<http://www.iinteractive.com>
447 This library is free software; you can redistribute it and/or modify
448 it under the same terms as Perl itself.