misc crap;
[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
2eb717d5 109 use Class::MOP ':universal';
110
111 package Foo;
112
113 Foo->meta->add_method('foo' => sub { ... });
94b19069 114
115=head1 DESCRIPTON
116
117This module is an attempt to create a meta object protocol for the
118Perl 5 object system. It makes no attempt to change the behavior or
119characteristics of the Perl 5 object system, only to create a
27e31eaf 120protocol for its manipulation and introspection.
94b19069 121
122That said, it does attempt to create the tools for building a rich
123set of extensions to the Perl 5 object system. Every attempt has been
124made for these tools to keep to the spirit of the Perl 5 object
125system that we all know and love.
126
bfe4d0fc 127=head2 What is a Meta Object Protocol?
128
129A meta object protocol is an API to an object system.
130
131To be more specific, it is a set of abstractions of the components of
132an object system (typically things like; classes, object, methods,
133object attributes, etc.). These abstractions can then be used to both
134inspect and manipulate the object system which they describe.
135
136It can be said that there are two MOPs for any object system; the
137implicit MOP, and the explicit MOP. The implicit MOP handles things
138like method dispatch or inheritance, which happen automatically as
139part of how the object system works. The explicit MOP typically
140handles the introspection/reflection features of the object system.
141All object systems have implicit MOPs, without one, they would not
142work. Explict MOPs however as less common, and depending on the
143language can vary from restrictive (Reflection in Java or C#) to
144wide open (CLOS is a perfect example).
145
e16da3e6 146=head2 Yet Another Class Builder!! Why?
147
148This is B<not> a class builder so much as it is a I<class builder
149B<builder>>. My intent is that an end user does not use this module
150directly, but instead this module is used by module authors to
151build extensions and features onto the Perl 5 object system.
152
94b19069 153=head2 Who is this module for?
154
155This module is specifically for anyone who has ever created or
156wanted to create a module for the Class:: namespace. The tools which
157this module will provide will hopefully make it easier to do more
158complex things with Perl 5 classes by removing such barriers as
159the need to hack the symbol tables, or understand the fine details
160of method dispatch.
161
bfe4d0fc 162=head2 What changes do I have to make to use this module?
163
2eb717d5 164This module was designed to be as unintrusive as possible. Many of
bfe4d0fc 165it's features are accessible without B<any> change to your existsing
166code at all. It is meant to be a compliment to your existing code and
2eb717d5 167not an intrusion on your code base. Unlike many other B<Class::>
168modules, this module does require you subclass it, or even that you
169C<use> it in within your module's package.
bfe4d0fc 170
2eb717d5 171The only features which requires additions to your code are the
172attribute handling and instance construction features, and these are
173both optional features as well. The only reason for this is because
174Perl 5's object system does not actually have these features built
175in. More information about this feature can be found below.
bfe4d0fc 176
177=head2 A Note about Performance?
178
179It is a common misconception that explict MOPs are performance drains.
180But this is not a universal truth at all, it is an side-effect of
181specific implementations. For instance, using Java reflection is much
182slower because the JVM cannot take advantage of any compiler
183optimizations, and the JVM has to deal with much more runtime type
184information as well. Reflection in C# is marginally better as it was
185designed into the language and runtime (the CLR). In contrast, CLOS
186(the Common Lisp Object System) was built to support an explicit MOP,
187and so performance is tuned for it.
188
189This library in particular does it's absolute best to avoid putting
2eb717d5 190B<any> drain at all upon your code's performance. In fact, by itself
191it does nothing to affect your existing code. So you only pay for
192what you actually use.
bfe4d0fc 193
94b19069 194=head1 PROTOCOLS
195
196The protocol is divided into 3 main sub-protocols:
197
198=over 4
199
200=item The Class protocol
201
202This provides a means of manipulating and introspecting a Perl 5
203class. It handles all of symbol table hacking for you, and provides
204a rich set of methods that go beyond simple package introspection.
205
552e3d24 206See L<Class::MOP::Class> for more details.
207
94b19069 208=item The Attribute protocol
209
210This provides a consistent represenation for an attribute of a
211Perl 5 class. Since there are so many ways to create and handle
212atttributes in Perl 5 OO, this attempts to provide as much of a
213unified approach as possible, while giving the freedom and
214flexibility to subclass for specialization.
215
552e3d24 216See L<Class::MOP::Attribute> for more details.
217
94b19069 218=item The Method protocol
219
220This provides a means of manipulating and introspecting methods in
221the Perl 5 object system. As with attributes, there are many ways to
222approach this topic, so we try to keep it pretty basic, while still
223making it possible to extend the system in many ways.
224
552e3d24 225See L<Class::MOP::Method> for more details.
94b19069 226
227=back
228
7184ca14 229head1 BUGS
230
231All complex software has bugs lurking in it, and this module is no
232exception. If you find a bug please either email me, or add the bug
233to cpan-RT.
234
552e3d24 235=head1 SEE ALSO
8b978dd5 236
552e3d24 237=head2 Books
8b978dd5 238
239=over 4
240
552e3d24 241=item "The Art of the Meta Object Protocol"
8b978dd5 242
552e3d24 243=item "Advances in Object-Oriented Metalevel Architecture and Reflection"
8b978dd5 244
b51af7f9 245=item "Putting MetaClasses to Work"
246
94b19069 247=back
248
552e3d24 249=head2 Prior Art
8b978dd5 250
251=over 4
252
7184ca14 253=item The Perl 6 MetaModel work in the Pugs project
8b978dd5 254
255=over 4
256
552e3d24 257=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
8b978dd5 258
552e3d24 259=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
8b978dd5 260
261=back
262
94b19069 263=back
264
265=head1 AUTHOR
266
267Stevan Little E<gt>stevan@iinteractive.comE<lt>
268
552e3d24 269Rob Kinyon E<gt>rob@iinteractive.comE<lt>
270
94b19069 271=head1 COPYRIGHT AND LICENSE
272
273Copyright 2006 by Infinity Interactive, Inc.
274
275L<http://www.iinteractive.com>
276
277This library is free software; you can redistribute it and/or modify
278it under the same terms as Perl itself.
279
280=cut