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' => (
52 init_arg => ':package'
56 Class::MOP::Class->meta->add_attribute(
57 Class::MOP::Attribute->new('%:attributes' => (
58 init_arg => ':attributes',
63 Class::MOP::Class->meta->add_attribute(
64 Class::MOP::Attribute->new('$:attribute_metaclass' => (
65 init_arg => ':attribute_metaclass',
66 default => 'Class::MOP::Attribute',
70 Class::MOP::Class->meta->add_attribute(
71 Class::MOP::Attribute->new('$:method_metaclass' => (
72 init_arg => ':method_metaclass',
73 default => 'Class::MOP::Method',
77 ## Class::MOP::Attribute
79 Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('name'));
80 Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('accessor'));
81 Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('reader'));
82 Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('writer'));
83 Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('predicate'));
84 Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('init_arg'));
85 Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('default'));
87 # NOTE: (meta-circularity)
88 # This should be one of the last things done
89 # it will "tie the knot" with Class::MOP::Attribute
90 # so that it uses the attributes meta-objects
91 # to construct itself.
92 Class::MOP::Attribute->meta->add_method('new' => sub {
97 (defined $name && $name)
98 || confess "You must provide a name for the attribute";
99 (!exists $options{reader} && !exists $options{writer})
100 || confess "You cannot declare an accessor and reader and/or writer functions"
101 if exists $options{accessor};
103 bless $class->meta->construct_instance(name => $name, %options) => $class;
114 Class::MOP - A Meta Object Protocol for Perl 5
118 # ... This will come later, for now see
119 # the other SYNOPSIS for more information
123 This module is an attempt to create a meta object protocol for the
124 Perl 5 object system. It makes no attempt to change the behavior or
125 characteristics of the Perl 5 object system, only to create a
126 protocol for its manipulation and introspection.
128 That said, it does attempt to create the tools for building a rich
129 set of extensions to the Perl 5 object system. Every attempt has been
130 made for these tools to keep to the spirit of the Perl 5 object
131 system that we all know and love.
133 =head2 What is a Meta Object Protocol?
135 A meta object protocol is an API to an object system.
137 To be more specific, it is a set of abstractions of the components of
138 an object system (typically things like; classes, object, methods,
139 object attributes, etc.). These abstractions can then be used to both
140 inspect and manipulate the object system which they describe.
142 It can be said that there are two MOPs for any object system; the
143 implicit MOP, and the explicit MOP. The implicit MOP handles things
144 like method dispatch or inheritance, which happen automatically as
145 part of how the object system works. The explicit MOP typically
146 handles the introspection/reflection features of the object system.
147 All object systems have implicit MOPs, without one, they would not
148 work. Explict MOPs however as less common, and depending on the
149 language can vary from restrictive (Reflection in Java or C#) to
150 wide open (CLOS is a perfect example).
152 =head2 Yet Another Class Builder!! Why?
154 This is B<not> a class builder so much as it is a I<class builder
155 B<builder>>. My intent is that an end user does not use this module
156 directly, but instead this module is used by module authors to
157 build extensions and features onto the Perl 5 object system.
159 =head2 Who is this module for?
161 This module is specifically for anyone who has ever created or
162 wanted to create a module for the Class:: namespace. The tools which
163 this module will provide will hopefully make it easier to do more
164 complex things with Perl 5 classes by removing such barriers as
165 the need to hack the symbol tables, or understand the fine details
168 =head2 What changes do I have to make to use this module?
170 This module was designed to be as unintrusive as possible. Many of
171 its features are accessible without B<any> change to your existsing
172 code at all. It is meant to be a compliment to your existing code and
173 not an intrusion on your code base. Unlike many other B<Class::>
174 modules, this module B<does not> require you subclass it, or even that
175 you C<use> it in within your module's package.
177 The only features which requires additions to your code are the
178 attribute handling and instance construction features, and these are
179 both completely optional features. The only reason for this is because
180 Perl 5's object system does not actually have these features built
181 in. More information about this feature can be found below.
183 =head2 A Note about Performance?
185 It is a common misconception that explict MOPs are performance drains.
186 But this is not a universal truth at all, it is an side-effect of
187 specific implementations. For instance, using Java reflection is much
188 slower because the JVM cannot take advantage of any compiler
189 optimizations, and the JVM has to deal with much more runtime type
190 information as well. Reflection in C# is marginally better as it was
191 designed into the language and runtime (the CLR). In contrast, CLOS
192 (the Common Lisp Object System) was built to support an explicit MOP,
193 and so performance is tuned for it.
195 This library in particular does it's absolute best to avoid putting
196 B<any> drain at all upon your code's performance. In fact, by itself
197 it does nothing to affect your existing code. So you only pay for
198 what you actually use.
202 The protocol is divided into 3 main sub-protocols:
206 =item The Class protocol
208 This provides a means of manipulating and introspecting a Perl 5
209 class. It handles all of symbol table hacking for you, and provides
210 a rich set of methods that go beyond simple package introspection.
212 See L<Class::MOP::Class> for more details.
214 =item The Attribute protocol
216 This provides a consistent represenation for an attribute of a
217 Perl 5 class. Since there are so many ways to create and handle
218 atttributes in Perl 5 OO, this attempts to provide as much of a
219 unified approach as possible, while giving the freedom and
220 flexibility to subclass for specialization.
222 See L<Class::MOP::Attribute> for more details.
224 =item The Method protocol
226 This provides a means of manipulating and introspecting methods in
227 the Perl 5 object system. As with attributes, there are many ways to
228 approach this topic, so we try to keep it pretty basic, while still
229 making it possible to extend the system in many ways.
231 See L<Class::MOP::Method> for more details.
239 There are very few books out on Meta Object Protocols and Metaclasses
240 because it is such an esoteric topic. The following books are really
241 the only ones I have found. If you know of any more, B<I<please>>
242 email me and let me know, I would love to hear about them.
246 =item "The Art of the Meta Object Protocol"
248 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
250 =item "Putting MetaClasses to Work"
252 =item "Smalltalk: The Language"
260 =item The Perl 6 MetaModel work in the Pugs project
264 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
266 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
272 =head1 SIMILAR MODULES
274 As I have said above, this module is a class-builder-builder, so it is
275 not the same thing as modules like L<Class::Accessor> and
276 L<Class::MethodMaker>. That being said there are very few modules on CPAN
277 with similar goals to this module. The one I have found which is most
278 like this module is L<Class::Meta>, although it's philosophy is very
279 different from this module.
281 To start with, it provides wrappers around common Perl data types, and even
282 extends those types with more specific subtypes. This module does not
283 go into that area at all.
285 L<Class::Meta> also seems to create it's own custom meta-object protocol,
286 which is both more restrictive and more featureful than the vanilla
287 Perl 5 one. This module attempts to model the existing Perl 5 MOP as it is.
289 It's introspection capabilities also seem to be heavily rooted in this
290 custom MOP, so that you can only introspect classes which are already
291 created with L<Class::Meta>. This module does not make such restictions.
293 Now, all this said, L<Class::Meta> is much more featureful than B<Class::MOP>
294 would ever try to be. But B<Class::MOP> has some features which L<Class::Meta>
295 could not easily implement. It would be very possible to completely re-implement
296 L<Class::Meta> using B<Class::MOP> and bring some of these features to
297 L<Class::Meta> though.
299 But in the end, this module's admitedly ambitious goals have no direct equal
300 on CPAN since surely no one has been crazy enough to try something as silly
301 as this ;) until now.
305 All complex software has bugs lurking in it, and this module is no
306 exception. If you find a bug please either email me, or add the bug
309 =head1 ACKNOWLEDGEMENTS
313 =item Rob Kinyon E<lt>rob@iinteractive.comE<gt>
315 Thanks to Rob for actually getting the development of this module kick-started.
321 Stevan Little E<lt>stevan@iinteractive.comE<gt>
323 =head1 COPYRIGHT AND LICENSE
325 Copyright 2006 by Infinity Interactive, Inc.
327 L<http://www.iinteractive.com>
329 This library is free software; you can redistribute it and/or modify
330 it under the same terms as Perl itself.