Commit | Line | Data |
94b19069 |
1 | |
2 | package Class::MOP; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
727919c5 |
7 | use Carp 'confess'; |
aa448b16 |
8 | use Scalar::Util (); |
8b978dd5 |
9 | |
2eb717d5 |
10 | use Class::MOP::Class; |
11 | use Class::MOP::Attribute; |
12 | use Class::MOP::Method; |
13 | |
3bf7644b |
14 | our $VERSION = '0.10'; |
94b19069 |
15 | |
aa448b16 |
16 | ## ---------------------------------------------------------------------------- |
17 | ## Setting up our environment ... |
18 | ## ---------------------------------------------------------------------------- |
19 | ## Class::MOP needs to have a few things in the global perl environment so |
20 | ## that it can operate effectively. Those things are done here. |
21 | ## ---------------------------------------------------------------------------- |
22 | |
3bf7644b |
23 | # ... nothing yet actually ;) |
8b978dd5 |
24 | |
b51af7f9 |
25 | ## ---------------------------------------------------------------------------- |
26 | ## Bootstrapping |
27 | ## ---------------------------------------------------------------------------- |
28 | ## The code below here is to bootstrap our MOP with itself. This is also |
29 | ## sometimes called "tying the knot". By doing this, we make it much easier |
30 | ## to extend the MOP through subclassing and such since now you can use the |
31 | ## MOP itself to extend itself. |
32 | ## |
33 | ## Yes, I know, thats weird and insane, but it's a good thing, trust me :) |
34 | ## ---------------------------------------------------------------------------- |
727919c5 |
35 | |
36 | # We need to add in the meta-attributes here so that |
37 | # any subclass of Class::MOP::* will be able to |
38 | # inherit them using &construct_instance |
39 | |
40 | ## Class::MOP::Class |
41 | |
42 | Class::MOP::Class->meta->add_attribute( |
351bd7d4 |
43 | Class::MOP::Attribute->new('$:package' => ( |
7b31baf4 |
44 | reader => 'name', |
45 | init_arg => ':package', |
727919c5 |
46 | )) |
47 | ); |
48 | |
49 | Class::MOP::Class->meta->add_attribute( |
351bd7d4 |
50 | Class::MOP::Attribute->new('%:attributes' => ( |
7b31baf4 |
51 | reader => 'get_attribute_map', |
351bd7d4 |
52 | init_arg => ':attributes', |
727919c5 |
53 | default => sub { {} } |
54 | )) |
55 | ); |
56 | |
351bd7d4 |
57 | Class::MOP::Class->meta->add_attribute( |
58 | Class::MOP::Attribute->new('$:attribute_metaclass' => ( |
7b31baf4 |
59 | reader => 'attribute_metaclass', |
351bd7d4 |
60 | init_arg => ':attribute_metaclass', |
61 | default => 'Class::MOP::Attribute', |
62 | )) |
63 | ); |
64 | |
65 | Class::MOP::Class->meta->add_attribute( |
66 | Class::MOP::Attribute->new('$:method_metaclass' => ( |
7b31baf4 |
67 | reader => 'method_metaclass', |
351bd7d4 |
68 | init_arg => ':method_metaclass', |
69 | default => 'Class::MOP::Method', |
70 | )) |
71 | ); |
72 | |
727919c5 |
73 | ## Class::MOP::Attribute |
74 | |
7b31baf4 |
75 | Class::MOP::Attribute->meta->add_attribute( |
76 | Class::MOP::Attribute->new('name' => ( |
77 | reader => 'name' |
78 | )) |
79 | ); |
80 | |
81 | Class::MOP::Attribute->meta->add_attribute( |
82 | Class::MOP::Attribute->new('associated_class' => ( |
83 | reader => 'associated_class' |
84 | )) |
85 | ); |
86 | |
87 | Class::MOP::Attribute->meta->add_attribute( |
88 | Class::MOP::Attribute->new('accessor' => ( |
89 | reader => 'accessor', |
90 | predicate => 'has_accessor', |
91 | )) |
92 | ); |
93 | |
94 | Class::MOP::Attribute->meta->add_attribute( |
95 | Class::MOP::Attribute->new('reader' => ( |
96 | reader => 'reader', |
97 | predicate => 'has_reader', |
98 | )) |
99 | ); |
100 | |
101 | Class::MOP::Attribute->meta->add_attribute( |
102 | Class::MOP::Attribute->new('writer' => ( |
103 | reader => 'writer', |
104 | predicate => 'has_writer', |
105 | )) |
106 | ); |
107 | |
108 | Class::MOP::Attribute->meta->add_attribute( |
109 | Class::MOP::Attribute->new('predicate' => ( |
110 | reader => 'predicate', |
111 | predicate => 'has_predicate', |
112 | )) |
113 | ); |
114 | |
115 | Class::MOP::Attribute->meta->add_attribute( |
116 | Class::MOP::Attribute->new('init_arg' => ( |
117 | reader => 'init_arg', |
118 | predicate => 'has_init_arg', |
119 | )) |
120 | ); |
121 | |
122 | Class::MOP::Attribute->meta->add_attribute( |
123 | Class::MOP::Attribute->new('default' => ( |
124 | # default has a custom 'reader' method ... |
125 | predicate => 'has_default', |
126 | )) |
127 | ); |
128 | |
727919c5 |
129 | |
130 | # NOTE: (meta-circularity) |
131 | # This should be one of the last things done |
132 | # it will "tie the knot" with Class::MOP::Attribute |
133 | # so that it uses the attributes meta-objects |
134 | # to construct itself. |
135 | Class::MOP::Attribute->meta->add_method('new' => sub { |
136 | my $class = shift; |
137 | my $name = shift; |
138 | my %options = @_; |
139 | |
140 | (defined $name && $name) |
141 | || confess "You must provide a name for the attribute"; |
5659d76e |
142 | $options{init_arg} = $name |
143 | if not exists $options{init_arg}; |
651955fb |
144 | |
5659d76e |
145 | # return the new object |
146 | $class->meta->new_object(name => $name, %options); |
147 | }); |
148 | |
149 | Class::MOP::Attribute->meta->add_method('clone' => sub { |
a740253a |
150 | my $self = shift; |
151 | my $class = $self->associated_class; |
152 | $self->detach_from_class() if defined $class; |
153 | my $clone = $self->meta->clone_object($self, @_); |
154 | if (defined $class) { |
155 | $self->attach_to_class($class); |
156 | $clone->attach_to_class($class); |
157 | } |
158 | return $clone; |
727919c5 |
159 | }); |
160 | |
94b19069 |
161 | 1; |
162 | |
163 | __END__ |
164 | |
165 | =pod |
166 | |
167 | =head1 NAME |
168 | |
169 | Class::MOP - A Meta Object Protocol for Perl 5 |
170 | |
171 | =head1 SYNOPSIS |
172 | |
a2e85e6c |
173 | # ... This will come later, for now see |
174 | # the other SYNOPSIS for more information |
94b19069 |
175 | |
176 | =head1 DESCRIPTON |
177 | |
178 | This module is an attempt to create a meta object protocol for the |
179 | Perl 5 object system. It makes no attempt to change the behavior or |
180 | characteristics of the Perl 5 object system, only to create a |
27e31eaf |
181 | protocol for its manipulation and introspection. |
94b19069 |
182 | |
183 | That said, it does attempt to create the tools for building a rich |
184 | set of extensions to the Perl 5 object system. Every attempt has been |
185 | made for these tools to keep to the spirit of the Perl 5 object |
186 | system that we all know and love. |
187 | |
bfe4d0fc |
188 | =head2 What is a Meta Object Protocol? |
189 | |
190 | A meta object protocol is an API to an object system. |
191 | |
192 | To be more specific, it is a set of abstractions of the components of |
193 | an object system (typically things like; classes, object, methods, |
194 | object attributes, etc.). These abstractions can then be used to both |
195 | inspect and manipulate the object system which they describe. |
196 | |
197 | It can be said that there are two MOPs for any object system; the |
198 | implicit MOP, and the explicit MOP. The implicit MOP handles things |
199 | like method dispatch or inheritance, which happen automatically as |
200 | part of how the object system works. The explicit MOP typically |
201 | handles the introspection/reflection features of the object system. |
202 | All object systems have implicit MOPs, without one, they would not |
203 | work. Explict MOPs however as less common, and depending on the |
204 | language can vary from restrictive (Reflection in Java or C#) to |
205 | wide open (CLOS is a perfect example). |
206 | |
e16da3e6 |
207 | =head2 Yet Another Class Builder!! Why? |
208 | |
209 | This is B<not> a class builder so much as it is a I<class builder |
210 | B<builder>>. My intent is that an end user does not use this module |
211 | directly, but instead this module is used by module authors to |
212 | build extensions and features onto the Perl 5 object system. |
213 | |
94b19069 |
214 | =head2 Who is this module for? |
215 | |
216 | This module is specifically for anyone who has ever created or |
217 | wanted to create a module for the Class:: namespace. The tools which |
218 | this module will provide will hopefully make it easier to do more |
219 | complex things with Perl 5 classes by removing such barriers as |
220 | the need to hack the symbol tables, or understand the fine details |
221 | of method dispatch. |
222 | |
bfe4d0fc |
223 | =head2 What changes do I have to make to use this module? |
224 | |
2eb717d5 |
225 | This module was designed to be as unintrusive as possible. Many of |
343203ee |
226 | its features are accessible without B<any> change to your existsing |
bfe4d0fc |
227 | code at all. It is meant to be a compliment to your existing code and |
2eb717d5 |
228 | not an intrusion on your code base. Unlike many other B<Class::> |
a2e85e6c |
229 | modules, this module B<does not> require you subclass it, or even that |
230 | you C<use> it in within your module's package. |
bfe4d0fc |
231 | |
2eb717d5 |
232 | The only features which requires additions to your code are the |
233 | attribute handling and instance construction features, and these are |
a2e85e6c |
234 | both completely optional features. The only reason for this is because |
2eb717d5 |
235 | Perl 5's object system does not actually have these features built |
236 | in. More information about this feature can be found below. |
bfe4d0fc |
237 | |
238 | =head2 A Note about Performance? |
239 | |
240 | It is a common misconception that explict MOPs are performance drains. |
241 | But this is not a universal truth at all, it is an side-effect of |
242 | specific implementations. For instance, using Java reflection is much |
243 | slower because the JVM cannot take advantage of any compiler |
244 | optimizations, and the JVM has to deal with much more runtime type |
245 | information as well. Reflection in C# is marginally better as it was |
246 | designed into the language and runtime (the CLR). In contrast, CLOS |
247 | (the Common Lisp Object System) was built to support an explicit MOP, |
248 | and so performance is tuned for it. |
249 | |
250 | This library in particular does it's absolute best to avoid putting |
2eb717d5 |
251 | B<any> drain at all upon your code's performance. In fact, by itself |
252 | it does nothing to affect your existing code. So you only pay for |
253 | what you actually use. |
bfe4d0fc |
254 | |
550d56db |
255 | =head2 About Metaclass compatibility |
256 | |
257 | This module makes sure that all metaclasses created are both upwards |
258 | and downwards compatible. The topic of metaclass compatibility is |
259 | highly esoteric and is something only encountered when doing deep and |
260 | involved metaclass hacking. There are two basic kinds of metaclass |
261 | incompatibility; upwards and downwards. |
262 | |
263 | Upwards metaclass compatibility means that the metaclass of a |
264 | given class is either the same as (or a subclass of) all of the |
265 | class's ancestors. |
266 | |
267 | Downward metaclass compatibility means that the metaclasses of a |
268 | given class's anscestors are all either the same as (or a subclass |
269 | of) that metaclass. |
270 | |
271 | Here is a diagram showing a set of two classes (C<A> and C<B>) and |
272 | two metaclasses (C<Meta::A> and C<Meta::B>) which have correct |
273 | metaclass compatibility both upwards and downwards. |
274 | |
275 | +---------+ +---------+ |
276 | | Meta::A |<----| Meta::B | <....... (instance of ) |
277 | +---------+ +---------+ <------- (inherits from) |
278 | ^ ^ |
279 | : : |
280 | +---------+ +---------+ |
281 | | A |<----| B | |
282 | +---------+ +---------+ |
283 | |
284 | As I said this is a highly esoteric topic and one you will only run |
285 | into if you do a lot of subclassing of B<Class::MOP::Class>. If you |
286 | are interested in why this is an issue see the paper |
287 | I<Uniform and safe metaclass composition> linked to in the |
288 | L<SEE ALSO> section of this document. |
289 | |
aa448b16 |
290 | =head2 Using custom metaclasses |
291 | |
292 | Always use the metaclass pragma when using a custom metaclass, this |
293 | will ensure the proper initialization order and not accidentely |
294 | create an incorrect type of metaclass for you. This is a very rare |
295 | problem, and one which can only occur if you are doing deep metaclass |
296 | programming. So in other words, don't worry about it. |
297 | |
94b19069 |
298 | =head1 PROTOCOLS |
299 | |
300 | The protocol is divided into 3 main sub-protocols: |
301 | |
302 | =over 4 |
303 | |
304 | =item The Class protocol |
305 | |
306 | This provides a means of manipulating and introspecting a Perl 5 |
307 | class. It handles all of symbol table hacking for you, and provides |
308 | a rich set of methods that go beyond simple package introspection. |
309 | |
552e3d24 |
310 | See L<Class::MOP::Class> for more details. |
311 | |
94b19069 |
312 | =item The Attribute protocol |
313 | |
314 | This provides a consistent represenation for an attribute of a |
315 | Perl 5 class. Since there are so many ways to create and handle |
316 | atttributes in Perl 5 OO, this attempts to provide as much of a |
317 | unified approach as possible, while giving the freedom and |
318 | flexibility to subclass for specialization. |
319 | |
552e3d24 |
320 | See L<Class::MOP::Attribute> for more details. |
321 | |
94b19069 |
322 | =item The Method protocol |
323 | |
324 | This provides a means of manipulating and introspecting methods in |
325 | the Perl 5 object system. As with attributes, there are many ways to |
326 | approach this topic, so we try to keep it pretty basic, while still |
327 | making it possible to extend the system in many ways. |
328 | |
552e3d24 |
329 | See L<Class::MOP::Method> for more details. |
94b19069 |
330 | |
331 | =back |
332 | |
552e3d24 |
333 | =head1 SEE ALSO |
8b978dd5 |
334 | |
552e3d24 |
335 | =head2 Books |
8b978dd5 |
336 | |
a2e85e6c |
337 | There are very few books out on Meta Object Protocols and Metaclasses |
338 | because it is such an esoteric topic. The following books are really |
339 | the only ones I have found. If you know of any more, B<I<please>> |
340 | email me and let me know, I would love to hear about them. |
341 | |
8b978dd5 |
342 | =over 4 |
343 | |
552e3d24 |
344 | =item "The Art of the Meta Object Protocol" |
8b978dd5 |
345 | |
552e3d24 |
346 | =item "Advances in Object-Oriented Metalevel Architecture and Reflection" |
8b978dd5 |
347 | |
b51af7f9 |
348 | =item "Putting MetaClasses to Work" |
349 | |
a2e85e6c |
350 | =item "Smalltalk: The Language" |
351 | |
94b19069 |
352 | =back |
353 | |
550d56db |
354 | =head2 Papers |
355 | |
356 | =over 4 |
357 | |
358 | =item Uniform and safe metaclass composition |
359 | |
360 | An excellent paper by the people who brought us the original Traits paper. |
361 | This paper is on how Traits can be used to do safe metaclass composition, |
362 | and offers an excellent introduction section which delves into the topic of |
363 | metaclass compatibility. |
364 | |
365 | L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf> |
366 | |
367 | =item Safe Metaclass Programming |
368 | |
369 | This paper seems to precede the above paper, and propose a mix-in based |
370 | approach as opposed to the Traits based approach. Both papers have similar |
371 | information on the metaclass compatibility problem space. |
372 | |
373 | L<http://citeseer.ist.psu.edu/37617.html> |
374 | |
375 | =back |
376 | |
552e3d24 |
377 | =head2 Prior Art |
8b978dd5 |
378 | |
379 | =over 4 |
380 | |
7184ca14 |
381 | =item The Perl 6 MetaModel work in the Pugs project |
8b978dd5 |
382 | |
383 | =over 4 |
384 | |
552e3d24 |
385 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel> |
8b978dd5 |
386 | |
552e3d24 |
387 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace> |
8b978dd5 |
388 | |
389 | =back |
390 | |
94b19069 |
391 | =back |
392 | |
a2e85e6c |
393 | =head1 SIMILAR MODULES |
394 | |
395 | As I have said above, this module is a class-builder-builder, so it is |
396 | not the same thing as modules like L<Class::Accessor> and |
397 | L<Class::MethodMaker>. That being said there are very few modules on CPAN |
398 | with similar goals to this module. The one I have found which is most |
550d56db |
399 | like this module is L<Class::Meta>, although it's philosophy and the MOP it |
400 | creates are very different from this modules. |
94b19069 |
401 | |
a2e85e6c |
402 | =head1 BUGS |
403 | |
404 | All complex software has bugs lurking in it, and this module is no |
405 | exception. If you find a bug please either email me, or add the bug |
406 | to cpan-RT. |
407 | |
22286063 |
408 | =head1 CODE COVERAGE |
409 | |
410 | I use L<Devel::Cover> to test the code coverage of my tests, below is the |
411 | L<Devel::Cover> report on this module's test suite. |
412 | |
413 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
414 | File stmt bran cond sub pod time total |
415 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
416 | Class/MOP.pm 100.0 100.0 100.0 100.0 n/a 21.4 100.0 |
417 | Class/MOP/Attribute.pm 100.0 100.0 88.9 100.0 100.0 27.1 99.3 |
418 | Class/MOP/Class.pm 100.0 100.0 93.7 100.0 100.0 44.8 99.1 |
419 | Class/MOP/Method.pm 100.0 100.0 83.3 100.0 100.0 4.8 97.1 |
420 | metaclass.pm 100.0 100.0 80.0 100.0 n/a 1.9 97.3 |
421 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
422 | Total 100.0 100.0 92.2 100.0 100.0 100.0 99.0 |
423 | ---------------------------- ------ ------ ------ ------ ------ ------ ------ |
424 | |
a2e85e6c |
425 | =head1 ACKNOWLEDGEMENTS |
426 | |
427 | =over 4 |
428 | |
429 | =item Rob Kinyon E<lt>rob@iinteractive.comE<gt> |
430 | |
431 | Thanks to Rob for actually getting the development of this module kick-started. |
432 | |
433 | =back |
434 | |
435 | =head1 AUTHOR |
94b19069 |
436 | |
a2e85e6c |
437 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
552e3d24 |
438 | |
94b19069 |
439 | =head1 COPYRIGHT AND LICENSE |
440 | |
441 | Copyright 2006 by Infinity Interactive, Inc. |
442 | |
443 | L<http://www.iinteractive.com> |
444 | |
445 | This library is free software; you can redistribute it and/or modify |
446 | it under the same terms as Perl itself. |
447 | |
448 | =cut |