many docs additions and a new test
[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
94b19069 14our $VERSION = '0.01';
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 }
24}
8b978dd5 25
b51af7f9 26## ----------------------------------------------------------------------------
27## Bootstrapping
28## ----------------------------------------------------------------------------
29## The code below here is to bootstrap our MOP with itself. This is also
30## sometimes called "tying the knot". By doing this, we make it much easier
31## to extend the MOP through subclassing and such since now you can use the
32## MOP itself to extend itself.
33##
34## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
35## ----------------------------------------------------------------------------
727919c5 36
37# We need to add in the meta-attributes here so that
38# any subclass of Class::MOP::* will be able to
39# inherit them using &construct_instance
40
41## Class::MOP::Class
42
43Class::MOP::Class->meta->add_attribute(
44 Class::MOP::Attribute->new('$:pkg' => (
45 init_arg => ':pkg'
46 ))
47);
48
49Class::MOP::Class->meta->add_attribute(
50 Class::MOP::Attribute->new('%:attrs' => (
51 init_arg => ':attrs',
52 default => sub { {} }
53 ))
54);
55
56## Class::MOP::Attribute
57
58Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('name'));
59Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('accessor'));
60Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('reader'));
61Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('writer'));
62Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('predicate'));
63Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('init_arg'));
64Class::MOP::Attribute->meta->add_attribute(Class::MOP::Attribute->new('default'));
65
66# NOTE: (meta-circularity)
67# This should be one of the last things done
68# it will "tie the knot" with Class::MOP::Attribute
69# so that it uses the attributes meta-objects
70# to construct itself.
71Class::MOP::Attribute->meta->add_method('new' => sub {
72 my $class = shift;
73 my $name = shift;
74 my %options = @_;
75
76 (defined $name && $name)
77 || confess "You must provide a name for the attribute";
78 (!exists $options{reader} && !exists $options{writer})
79 || confess "You cannot declare an accessor and reader and/or writer functions"
80 if exists $options{accessor};
81
82 bless $class->meta->construct_instance(name => $name, %options) => $class;
83});
84
85# NOTE: (meta-circularity)
86# This is how we "tie the knot" for the class
87# meta-objects. This is used to construct the
88# Class::MOP::Class instances after all the
89# bootstrapping is complete.
90Class::MOP::Class->meta->add_method('construct_class_instance' => sub {
91 my ($class, $package_name) = @_;
92 (defined $package_name && $package_name)
93 || confess "You must pass a package name";
94 bless Class::MOP::Class->meta->construct_instance(':pkg' => $package_name) => blessed($class) || $class
95});
96
94b19069 971;
98
99__END__
100
101=pod
102
103=head1 NAME
104
105Class::MOP - A Meta Object Protocol for Perl 5
106
107=head1 SYNOPSIS
108
fe122940 109 # ...
94b19069 110
111=head1 DESCRIPTON
112
113This module is an attempt to create a meta object protocol for the
114Perl 5 object system. It makes no attempt to change the behavior or
115characteristics of the Perl 5 object system, only to create a
27e31eaf 116protocol for its manipulation and introspection.
94b19069 117
118That said, it does attempt to create the tools for building a rich
119set of extensions to the Perl 5 object system. Every attempt has been
120made for these tools to keep to the spirit of the Perl 5 object
121system that we all know and love.
122
bfe4d0fc 123=head2 What is a Meta Object Protocol?
124
125A meta object protocol is an API to an object system.
126
127To be more specific, it is a set of abstractions of the components of
128an object system (typically things like; classes, object, methods,
129object attributes, etc.). These abstractions can then be used to both
130inspect and manipulate the object system which they describe.
131
132It can be said that there are two MOPs for any object system; the
133implicit MOP, and the explicit MOP. The implicit MOP handles things
134like method dispatch or inheritance, which happen automatically as
135part of how the object system works. The explicit MOP typically
136handles the introspection/reflection features of the object system.
137All object systems have implicit MOPs, without one, they would not
138work. Explict MOPs however as less common, and depending on the
139language can vary from restrictive (Reflection in Java or C#) to
140wide open (CLOS is a perfect example).
141
e16da3e6 142=head2 Yet Another Class Builder!! Why?
143
144This is B<not> a class builder so much as it is a I<class builder
145B<builder>>. My intent is that an end user does not use this module
146directly, but instead this module is used by module authors to
147build extensions and features onto the Perl 5 object system.
148
94b19069 149=head2 Who is this module for?
150
151This module is specifically for anyone who has ever created or
152wanted to create a module for the Class:: namespace. The tools which
153this module will provide will hopefully make it easier to do more
154complex things with Perl 5 classes by removing such barriers as
155the need to hack the symbol tables, or understand the fine details
156of method dispatch.
157
bfe4d0fc 158=head2 What changes do I have to make to use this module?
159
2eb717d5 160This module was designed to be as unintrusive as possible. Many of
bfe4d0fc 161it's features are accessible without B<any> change to your existsing
162code at all. It is meant to be a compliment to your existing code and
2eb717d5 163not an intrusion on your code base. Unlike many other B<Class::>
164modules, this module does require you subclass it, or even that you
165C<use> it in within your module's package.
bfe4d0fc 166
2eb717d5 167The only features which requires additions to your code are the
168attribute handling and instance construction features, and these are
169both optional features as well. The only reason for this is because
170Perl 5's object system does not actually have these features built
171in. More information about this feature can be found below.
bfe4d0fc 172
173=head2 A Note about Performance?
174
175It is a common misconception that explict MOPs are performance drains.
176But this is not a universal truth at all, it is an side-effect of
177specific implementations. For instance, using Java reflection is much
178slower because the JVM cannot take advantage of any compiler
179optimizations, and the JVM has to deal with much more runtime type
180information as well. Reflection in C# is marginally better as it was
181designed into the language and runtime (the CLR). In contrast, CLOS
182(the Common Lisp Object System) was built to support an explicit MOP,
183and so performance is tuned for it.
184
185This library in particular does it's absolute best to avoid putting
2eb717d5 186B<any> drain at all upon your code's performance. In fact, by itself
187it does nothing to affect your existing code. So you only pay for
188what you actually use.
bfe4d0fc 189
94b19069 190=head1 PROTOCOLS
191
192The protocol is divided into 3 main sub-protocols:
193
194=over 4
195
196=item The Class protocol
197
198This provides a means of manipulating and introspecting a Perl 5
199class. It handles all of symbol table hacking for you, and provides
200a rich set of methods that go beyond simple package introspection.
201
552e3d24 202See L<Class::MOP::Class> for more details.
203
94b19069 204=item The Attribute protocol
205
206This provides a consistent represenation for an attribute of a
207Perl 5 class. Since there are so many ways to create and handle
208atttributes in Perl 5 OO, this attempts to provide as much of a
209unified approach as possible, while giving the freedom and
210flexibility to subclass for specialization.
211
552e3d24 212See L<Class::MOP::Attribute> for more details.
213
94b19069 214=item The Method protocol
215
216This provides a means of manipulating and introspecting methods in
217the Perl 5 object system. As with attributes, there are many ways to
218approach this topic, so we try to keep it pretty basic, while still
219making it possible to extend the system in many ways.
220
552e3d24 221See L<Class::MOP::Method> for more details.
94b19069 222
223=back
224
7184ca14 225head1 BUGS
226
227All complex software has bugs lurking in it, and this module is no
228exception. If you find a bug please either email me, or add the bug
229to cpan-RT.
230
552e3d24 231=head1 SEE ALSO
8b978dd5 232
552e3d24 233=head2 Books
8b978dd5 234
235=over 4
236
552e3d24 237=item "The Art of the Meta Object Protocol"
8b978dd5 238
552e3d24 239=item "Advances in Object-Oriented Metalevel Architecture and Reflection"
8b978dd5 240
b51af7f9 241=item "Putting MetaClasses to Work"
242
94b19069 243=back
244
552e3d24 245=head2 Prior Art
8b978dd5 246
247=over 4
248
7184ca14 249=item The Perl 6 MetaModel work in the Pugs project
8b978dd5 250
251=over 4
252
552e3d24 253=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
8b978dd5 254
552e3d24 255=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
8b978dd5 256
257=back
258
94b19069 259=back
260
261=head1 AUTHOR
262
263Stevan Little E<gt>stevan@iinteractive.comE<lt>
264
552e3d24 265Rob Kinyon E<gt>rob@iinteractive.comE<lt>
266
94b19069 267=head1 COPYRIGHT AND LICENSE
268
269Copyright 2006 by Infinity Interactive, Inc.
270
271L<http://www.iinteractive.com>
272
273This library is free software; you can redistribute it and/or modify
274it under the same terms as Perl itself.
275
276=cut