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