more stuff
[gitmo/Class-MOP.git] / lib / Class / MOP.pm
CommitLineData
94b19069 1
2package Class::MOP;
3
4use strict;
5use warnings;
6
8b978dd5 7use Scalar::Util 'blessed';
727919c5 8use Carp 'confess';
8b978dd5 9
2eb717d5 10use Class::MOP::Class;
11use Class::MOP::Attribute;
12use Class::MOP::Method;
13
d6fbcd05 14our $VERSION = '0.04';
94b19069 15
2eb717d5 16sub import {
17 shift;
18 return unless @_;
19 if ($_[0] eq ':universal') {
20 *UNIVERSAL::meta = sub {
21 Class::MOP::Class->initialize(blessed($_[0]) || $_[0])
22 };
23 }
1a7ebbb3 24 else {
25 my $pkg = caller();
26 no strict 'refs';
27 *{$pkg . '::' . $_[0]} = sub {
28 Class::MOP::Class->initialize(blessed($_[0]) || $_[0])
29 };
30 }
2eb717d5 31}
8b978dd5 32
b51af7f9 33## ----------------------------------------------------------------------------
34## Bootstrapping
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.
40##
41## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
42## ----------------------------------------------------------------------------
727919c5 43
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
47
48## Class::MOP::Class
49
50Class::MOP::Class->meta->add_attribute(
51 Class::MOP::Attribute->new('$:pkg' => (
52 init_arg => ':pkg'
53 ))
54);
55
56Class::MOP::Class->meta->add_attribute(
57 Class::MOP::Attribute->new('%:attrs' => (
58 init_arg => ':attrs',
59 default => sub { {} }
60 ))
61);
62
63## Class::MOP::Attribute
64
65Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('name'));
66Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('accessor'));
67Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('reader'));
68Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('writer'));
69Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('predicate'));
70Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('init_arg'));
71Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('default'));
72
73# NOTE: (meta-circularity)
74# This should be one of the last things done
75# it will "tie the knot" with Class::MOP::Attribute
76# so that it uses the attributes meta-objects
77# to construct itself.
78Class::MOP::Attribute->meta->add_method('new' => sub {
79 my $class = shift;
80 my $name = shift;
81 my %options = @_;
82
83 (defined $name && $name)
84 || confess "You must provide a name for the attribute";
85 (!exists $options{reader} && !exists $options{writer})
86 || confess "You cannot declare an accessor and reader and/or writer functions"
87 if exists $options{accessor};
88
89 bless $class->meta->construct_instance(name => $name, %options) => $class;
90});
91
94b19069 921;
93
94__END__
95
96=pod
97
98=head1 NAME
99
100Class::MOP - A Meta Object Protocol for Perl 5
101
102=head1 SYNOPSIS
103
a2e85e6c 104 # ... This will come later, for now see
105 # the other SYNOPSIS for more information
94b19069 106
107=head1 DESCRIPTON
108
109This module is an attempt to create a meta object protocol for the
110Perl 5 object system. It makes no attempt to change the behavior or
111characteristics of the Perl 5 object system, only to create a
27e31eaf 112protocol for its manipulation and introspection.
94b19069 113
114That said, it does attempt to create the tools for building a rich
115set of extensions to the Perl 5 object system. Every attempt has been
116made for these tools to keep to the spirit of the Perl 5 object
117system that we all know and love.
118
bfe4d0fc 119=head2 What is a Meta Object Protocol?
120
121A meta object protocol is an API to an object system.
122
123To be more specific, it is a set of abstractions of the components of
124an object system (typically things like; classes, object, methods,
125object attributes, etc.). These abstractions can then be used to both
126inspect and manipulate the object system which they describe.
127
128It can be said that there are two MOPs for any object system; the
129implicit MOP, and the explicit MOP. The implicit MOP handles things
130like method dispatch or inheritance, which happen automatically as
131part of how the object system works. The explicit MOP typically
132handles the introspection/reflection features of the object system.
133All object systems have implicit MOPs, without one, they would not
134work. Explict MOPs however as less common, and depending on the
135language can vary from restrictive (Reflection in Java or C#) to
136wide open (CLOS is a perfect example).
137
e16da3e6 138=head2 Yet Another Class Builder!! Why?
139
140This is B<not> a class builder so much as it is a I<class builder
141B<builder>>. My intent is that an end user does not use this module
142directly, but instead this module is used by module authors to
143build extensions and features onto the Perl 5 object system.
144
94b19069 145=head2 Who is this module for?
146
147This module is specifically for anyone who has ever created or
148wanted to create a module for the Class:: namespace. The tools which
149this module will provide will hopefully make it easier to do more
150complex things with Perl 5 classes by removing such barriers as
151the need to hack the symbol tables, or understand the fine details
152of method dispatch.
153
bfe4d0fc 154=head2 What changes do I have to make to use this module?
155
2eb717d5 156This module was designed to be as unintrusive as possible. Many of
343203ee 157its features are accessible without B<any> change to your existsing
bfe4d0fc 158code at all. It is meant to be a compliment to your existing code and
2eb717d5 159not an intrusion on your code base. Unlike many other B<Class::>
a2e85e6c 160modules, this module B<does not> require you subclass it, or even that
161you C<use> it in within your module's package.
bfe4d0fc 162
2eb717d5 163The only features which requires additions to your code are the
164attribute handling and instance construction features, and these are
a2e85e6c 165both completely optional features. The only reason for this is because
2eb717d5 166Perl 5's object system does not actually have these features built
167in. More information about this feature can be found below.
bfe4d0fc 168
169=head2 A Note about Performance?
170
171It is a common misconception that explict MOPs are performance drains.
172But this is not a universal truth at all, it is an side-effect of
173specific implementations. For instance, using Java reflection is much
174slower because the JVM cannot take advantage of any compiler
175optimizations, and the JVM has to deal with much more runtime type
176information as well. Reflection in C# is marginally better as it was
177designed into the language and runtime (the CLR). In contrast, CLOS
178(the Common Lisp Object System) was built to support an explicit MOP,
179and so performance is tuned for it.
180
181This library in particular does it's absolute best to avoid putting
2eb717d5 182B<any> drain at all upon your code's performance. In fact, by itself
183it does nothing to affect your existing code. So you only pay for
184what you actually use.
bfe4d0fc 185
94b19069 186=head1 PROTOCOLS
187
188The protocol is divided into 3 main sub-protocols:
189
190=over 4
191
192=item The Class protocol
193
194This provides a means of manipulating and introspecting a Perl 5
195class. It handles all of symbol table hacking for you, and provides
196a rich set of methods that go beyond simple package introspection.
197
552e3d24 198See L<Class::MOP::Class> for more details.
199
94b19069 200=item The Attribute protocol
201
202This provides a consistent represenation for an attribute of a
203Perl 5 class. Since there are so many ways to create and handle
204atttributes in Perl 5 OO, this attempts to provide as much of a
205unified approach as possible, while giving the freedom and
206flexibility to subclass for specialization.
207
552e3d24 208See L<Class::MOP::Attribute> for more details.
209
94b19069 210=item The Method protocol
211
212This provides a means of manipulating and introspecting methods in
213the Perl 5 object system. As with attributes, there are many ways to
214approach this topic, so we try to keep it pretty basic, while still
215making it possible to extend the system in many ways.
216
552e3d24 217See L<Class::MOP::Method> for more details.
94b19069 218
219=back
220
552e3d24 221=head1 SEE ALSO
8b978dd5 222
552e3d24 223=head2 Books
8b978dd5 224
a2e85e6c 225There are very few books out on Meta Object Protocols and Metaclasses
226because it is such an esoteric topic. The following books are really
227the only ones I have found. If you know of any more, B<I<please>>
228email me and let me know, I would love to hear about them.
229
8b978dd5 230=over 4
231
552e3d24 232=item "The Art of the Meta Object Protocol"
8b978dd5 233
552e3d24 234=item "Advances in Object-Oriented Metalevel Architecture and Reflection"
8b978dd5 235
b51af7f9 236=item "Putting MetaClasses to Work"
237
a2e85e6c 238=item "Smalltalk: The Language"
239
94b19069 240=back
241
552e3d24 242=head2 Prior Art
8b978dd5 243
244=over 4
245
7184ca14 246=item The Perl 6 MetaModel work in the Pugs project
8b978dd5 247
248=over 4
249
552e3d24 250=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
8b978dd5 251
552e3d24 252=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
8b978dd5 253
254=back
255
94b19069 256=back
257
a2e85e6c 258=head1 SIMILAR MODULES
259
260As I have said above, this module is a class-builder-builder, so it is
261not the same thing as modules like L<Class::Accessor> and
262L<Class::MethodMaker>. That being said there are very few modules on CPAN
263with similar goals to this module. The one I have found which is most
264like this module is L<Class::Meta>, although it's philosophy is very
265different from this module.
266
267To start with, it provides wrappers around common Perl data types, and even
268extends those types with more specific subtypes. This module does not
269go into that area at all.
270
271L<Class::Meta> also seems to create it's own custom meta-object protocol,
272which is both more restrictive and more featureful than the vanilla
273Perl 5 one. This module attempts to model the existing Perl 5 MOP as it is.
274
275It's introspection capabilities also seem to be heavily rooted in this
276custom MOP, so that you can only introspect classes which are already
277created with L<Class::Meta>. This module does not make such restictions.
278
279Now, all this said, L<Class::Meta> is much more featureful than B<Class::MOP>
280would ever try to be. But B<Class::MOP> has some features which L<Class::Meta>
281could not easily implement. It would be very possible to completely re-implement
282L<Class::Meta> using B<Class::MOP> and bring some of these features to
283L<Class::Meta> though.
284
285But in the end, this module's admitedly ambitious goals have no direct equal
286on CPAN since surely no one has been crazy enough to try something as silly
287as this ;) until now.
94b19069 288
a2e85e6c 289=head1 BUGS
290
291All complex software has bugs lurking in it, and this module is no
292exception. If you find a bug please either email me, or add the bug
293to cpan-RT.
294
295=head1 ACKNOWLEDGEMENTS
296
297=over 4
298
299=item Rob Kinyon E<lt>rob@iinteractive.comE<gt>
300
301Thanks to Rob for actually getting the development of this module kick-started.
302
303=back
304
305=head1 AUTHOR
94b19069 306
a2e85e6c 307Stevan Little E<lt>stevan@iinteractive.comE<gt>
552e3d24 308
94b19069 309=head1 COPYRIGHT AND LICENSE
310
311Copyright 2006 by Infinity Interactive, Inc.
312
313L<http://www.iinteractive.com>
314
315This library is free software; you can redistribute it and/or modify
316it under the same terms as Perl itself.
317
318=cut