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