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