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