7 use Scalar::Util 'blessed';
10 use Class::MOP::Class;
11 use Class::MOP::Attribute;
12 use Class::MOP::Method;
14 our $VERSION = '0.06';
19 if ($_[0] eq ':universal') {
20 *UNIVERSAL::meta = sub {
21 Class::MOP::Class->initialize(blessed($_[0]) || $_[0])
27 *{$pkg . '::' . $_[0]} = sub {
28 Class::MOP::Class->initialize(blessed($_[0]) || $_[0])
33 ## ----------------------------------------------------------------------------
35 ## ----------------------------------------------------------------------------
36 ## The code below here is to bootstrap our MOP with itself. This is also
37 ## sometimes called "tying the knot". By doing this, we make it much easier
38 ## to extend the MOP through subclassing and such since now you can use the
39 ## MOP itself to extend itself.
41 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
42 ## ----------------------------------------------------------------------------
44 # We need to add in the meta-attributes here so that
45 # any subclass of Class::MOP::* will be able to
46 # inherit them using &construct_instance
50 Class::MOP::Class->meta->add_attribute(
51 Class::MOP::Attribute->new('$:package' => (
53 init_arg => ':package',
57 Class::MOP::Class->meta->add_attribute(
58 Class::MOP::Attribute->new('%:attributes' => (
59 reader => 'get_attribute_map',
60 init_arg => ':attributes',
65 Class::MOP::Class->meta->add_attribute(
66 Class::MOP::Attribute->new('$:attribute_metaclass' => (
67 reader => 'attribute_metaclass',
68 init_arg => ':attribute_metaclass',
69 default => 'Class::MOP::Attribute',
73 Class::MOP::Class->meta->add_attribute(
74 Class::MOP::Attribute->new('$:method_metaclass' => (
75 reader => 'method_metaclass',
76 init_arg => ':method_metaclass',
77 default => 'Class::MOP::Method',
81 ## Class::MOP::Attribute
83 Class::MOP::Attribute->meta->add_attribute(
84 Class::MOP::Attribute->new('name' => (
89 Class::MOP::Attribute->meta->add_attribute(
90 Class::MOP::Attribute->new('associated_class' => (
91 reader => 'associated_class'
95 Class::MOP::Attribute->meta->add_attribute(
96 Class::MOP::Attribute->new('accessor' => (
98 predicate => 'has_accessor',
102 Class::MOP::Attribute->meta->add_attribute(
103 Class::MOP::Attribute->new('reader' => (
105 predicate => 'has_reader',
109 Class::MOP::Attribute->meta->add_attribute(
110 Class::MOP::Attribute->new('writer' => (
112 predicate => 'has_writer',
116 Class::MOP::Attribute->meta->add_attribute(
117 Class::MOP::Attribute->new('predicate' => (
118 reader => 'predicate',
119 predicate => 'has_predicate',
123 Class::MOP::Attribute->meta->add_attribute(
124 Class::MOP::Attribute->new('init_arg' => (
125 reader => 'init_arg',
126 predicate => 'has_init_arg',
130 Class::MOP::Attribute->meta->add_attribute(
131 Class::MOP::Attribute->new('default' => (
132 # default has a custom 'reader' method ...
133 predicate => 'has_default',
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.
143 Class::MOP::Attribute->meta->add_method('new' => sub {
148 (defined $name && $name)
149 || confess "You must provide a name for the attribute";
150 $options{init_arg} = $name
151 if not exists $options{init_arg};
153 # return the new object
154 $class->meta->new_object(name => $name, %options);
157 Class::MOP::Attribute->meta->add_method('clone' => sub {
159 my $class = $self->associated_class;
160 $self->detach_from_class() if defined $class;
161 my $clone = $self->meta->clone_object($self, @_);
162 if (defined $class) {
163 $self->attach_to_class($class);
164 $clone->attach_to_class($class);
177 Class::MOP - A Meta Object Protocol for Perl 5
181 # ... This will come later, for now see
182 # the other SYNOPSIS for more information
186 This module is an attempt to create a meta object protocol for the
187 Perl 5 object system. It makes no attempt to change the behavior or
188 characteristics of the Perl 5 object system, only to create a
189 protocol for its manipulation and introspection.
191 That said, it does attempt to create the tools for building a rich
192 set of extensions to the Perl 5 object system. Every attempt has been
193 made for these tools to keep to the spirit of the Perl 5 object
194 system that we all know and love.
196 =head2 What is a Meta Object Protocol?
198 A meta object protocol is an API to an object system.
200 To be more specific, it is a set of abstractions of the components of
201 an object system (typically things like; classes, object, methods,
202 object attributes, etc.). These abstractions can then be used to both
203 inspect and manipulate the object system which they describe.
205 It can be said that there are two MOPs for any object system; the
206 implicit MOP, and the explicit MOP. The implicit MOP handles things
207 like method dispatch or inheritance, which happen automatically as
208 part of how the object system works. The explicit MOP typically
209 handles the introspection/reflection features of the object system.
210 All object systems have implicit MOPs, without one, they would not
211 work. Explict MOPs however as less common, and depending on the
212 language can vary from restrictive (Reflection in Java or C#) to
213 wide open (CLOS is a perfect example).
215 =head2 Yet Another Class Builder!! Why?
217 This is B<not> a class builder so much as it is a I<class builder
218 B<builder>>. My intent is that an end user does not use this module
219 directly, but instead this module is used by module authors to
220 build extensions and features onto the Perl 5 object system.
222 =head2 Who is this module for?
224 This module is specifically for anyone who has ever created or
225 wanted to create a module for the Class:: namespace. The tools which
226 this module will provide will hopefully make it easier to do more
227 complex things with Perl 5 classes by removing such barriers as
228 the need to hack the symbol tables, or understand the fine details
231 =head2 What changes do I have to make to use this module?
233 This module was designed to be as unintrusive as possible. Many of
234 its features are accessible without B<any> change to your existsing
235 code at all. It is meant to be a compliment to your existing code and
236 not an intrusion on your code base. Unlike many other B<Class::>
237 modules, this module B<does not> require you subclass it, or even that
238 you C<use> it in within your module's package.
240 The only features which requires additions to your code are the
241 attribute handling and instance construction features, and these are
242 both completely optional features. The only reason for this is because
243 Perl 5's object system does not actually have these features built
244 in. More information about this feature can be found below.
246 =head2 A Note about Performance?
248 It is a common misconception that explict MOPs are performance drains.
249 But this is not a universal truth at all, it is an side-effect of
250 specific implementations. For instance, using Java reflection is much
251 slower because the JVM cannot take advantage of any compiler
252 optimizations, and the JVM has to deal with much more runtime type
253 information as well. Reflection in C# is marginally better as it was
254 designed into the language and runtime (the CLR). In contrast, CLOS
255 (the Common Lisp Object System) was built to support an explicit MOP,
256 and so performance is tuned for it.
258 This library in particular does it's absolute best to avoid putting
259 B<any> drain at all upon your code's performance. In fact, by itself
260 it does nothing to affect your existing code. So you only pay for
261 what you actually use.
263 =head2 About Metaclass compatibility
265 This module makes sure that all metaclasses created are both upwards
266 and downwards compatible. The topic of metaclass compatibility is
267 highly esoteric and is something only encountered when doing deep and
268 involved metaclass hacking. There are two basic kinds of metaclass
269 incompatibility; upwards and downwards.
271 Upwards metaclass compatibility means that the metaclass of a
272 given class is either the same as (or a subclass of) all of the
275 Downward metaclass compatibility means that the metaclasses of a
276 given class's anscestors are all either the same as (or a subclass
279 Here is a diagram showing a set of two classes (C<A> and C<B>) and
280 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
281 metaclass compatibility both upwards and downwards.
283 +---------+ +---------+
284 | Meta::A |<----| Meta::B | <....... (instance of )
285 +---------+ +---------+ <------- (inherits from)
288 +---------+ +---------+
290 +---------+ +---------+
292 As I said this is a highly esoteric topic and one you will only run
293 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
294 are interested in why this is an issue see the paper
295 I<Uniform and safe metaclass composition> linked to in the
296 L<SEE ALSO> section of this document.
300 The protocol is divided into 3 main sub-protocols:
304 =item The Class protocol
306 This provides a means of manipulating and introspecting a Perl 5
307 class. It handles all of symbol table hacking for you, and provides
308 a rich set of methods that go beyond simple package introspection.
310 See L<Class::MOP::Class> for more details.
312 =item The Attribute protocol
314 This provides a consistent represenation for an attribute of a
315 Perl 5 class. Since there are so many ways to create and handle
316 atttributes in Perl 5 OO, this attempts to provide as much of a
317 unified approach as possible, while giving the freedom and
318 flexibility to subclass for specialization.
320 See L<Class::MOP::Attribute> for more details.
322 =item The Method protocol
324 This provides a means of manipulating and introspecting methods in
325 the Perl 5 object system. As with attributes, there are many ways to
326 approach this topic, so we try to keep it pretty basic, while still
327 making it possible to extend the system in many ways.
329 See L<Class::MOP::Method> for more details.
337 There are very few books out on Meta Object Protocols and Metaclasses
338 because it is such an esoteric topic. The following books are really
339 the only ones I have found. If you know of any more, B<I<please>>
340 email me and let me know, I would love to hear about them.
344 =item "The Art of the Meta Object Protocol"
346 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
348 =item "Putting MetaClasses to Work"
350 =item "Smalltalk: The Language"
358 =item Uniform and safe metaclass composition
360 An excellent paper by the people who brought us the original Traits paper.
361 This paper is on how Traits can be used to do safe metaclass composition,
362 and offers an excellent introduction section which delves into the topic of
363 metaclass compatibility.
365 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
367 =item Safe Metaclass Programming
369 This paper seems to precede the above paper, and propose a mix-in based
370 approach as opposed to the Traits based approach. Both papers have similar
371 information on the metaclass compatibility problem space.
373 L<http://citeseer.ist.psu.edu/37617.html>
381 =item The Perl 6 MetaModel work in the Pugs project
385 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
387 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
393 =head1 SIMILAR MODULES
395 As I have said above, this module is a class-builder-builder, so it is
396 not the same thing as modules like L<Class::Accessor> and
397 L<Class::MethodMaker>. That being said there are very few modules on CPAN
398 with similar goals to this module. The one I have found which is most
399 like this module is L<Class::Meta>, although it's philosophy and the MOP it
400 creates are very different from this modules.
404 All complex software has bugs lurking in it, and this module is no
405 exception. If you find a bug please either email me, or add the bug
408 =head1 ACKNOWLEDGEMENTS
412 =item Rob Kinyon E<lt>rob@iinteractive.comE<gt>
414 Thanks to Rob for actually getting the development of this module kick-started.
420 Stevan Little E<lt>stevan@iinteractive.comE<gt>
422 =head1 COPYRIGHT AND LICENSE
424 Copyright 2006 by Infinity Interactive, Inc.
426 L<http://www.iinteractive.com>
428 This library is free software; you can redistribute it and/or modify
429 it under the same terms as Perl itself.