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