Commit | Line | Data |
94b19069 |
1 | |
2 | package Class::MOP; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
5a2932cf |
7 | use 5.008; |
8 | |
3cf322a0 |
9 | use MRO::Compat; |
10 | |
4c105333 |
11 | use Carp 'confess'; |
b1a46f91 |
12 | use Scalar::Util 'weaken', 'reftype', 'blessed'; |
a8344505 |
13 | use Try::Tiny; |
fc4f8f91 |
14 | |
2611f98e |
15 | use Class::MOP::Mixin::AttributeBase; |
30bf0c82 |
16 | use Class::MOP::Mixin::HasAttributes; |
17 | use Class::MOP::Mixin::HasMethods; |
0531f510 |
18 | use Class::MOP::Class; |
19 | use Class::MOP::Attribute; |
20 | use Class::MOP::Method; |
21 | |
b1f5f41d |
22 | BEGIN { |
c7e28c19 |
23 | *IS_RUNNING_ON_5_10 = ($] < 5.009_005) |
11b56828 |
24 | ? sub () { 0 } |
c7e28c19 |
25 | : sub () { 1 }; |
46b23b44 |
26 | |
3ecd1b25 |
27 | # this is either part of core or set up appropriately by MRO::Compat |
28 | *check_package_cache_flag = \&mro::get_pkg_gen; |
b1f5f41d |
29 | } |
e0e4674a |
30 | |
ec52b37a |
31 | our $VERSION = '0.97'; |
8a7085c1 |
32 | our $XS_VERSION = $VERSION; |
d519662a |
33 | $VERSION = eval $VERSION; |
c7e28c19 |
34 | our $AUTHORITY = 'cpan:STEVAN'; |
9d655b6d |
35 | |
4bfa5ddb |
36 | require XSLoader; |
37 | XSLoader::load( __PACKAGE__, $XS_VERSION ); |
d7bda114 |
38 | |
39 | |
be7677c7 |
40 | { |
41 | # Metaclasses are singletons, so we cache them here. |
42 | # there is no need to worry about destruction though |
43 | # because they should die only when the program dies. |
44 | # After all, do package definitions even get reaped? |
247d5b3c |
45 | # Anonymous classes manage their own destruction. |
1d68af04 |
46 | my %METAS; |
47 | |
1d68af04 |
48 | sub get_all_metaclasses { %METAS } |
49 | sub get_all_metaclass_instances { values %METAS } |
50 | sub get_all_metaclass_names { keys %METAS } |
be7677c7 |
51 | sub get_metaclass_by_name { $METAS{$_[0]} } |
1d68af04 |
52 | sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] } |
53 | sub weaken_metaclass { weaken($METAS{$_[0]}) } |
be7677c7 |
54 | sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} } |
efc98200 |
55 | sub remove_metaclass_by_name { delete $METAS{$_[0]}; return } |
1d68af04 |
56 | |
86866fb5 |
57 | # This handles instances as well as class names |
58 | sub class_of { |
82999986 |
59 | return unless defined $_[0]; |
86866fb5 |
60 | my $class = blessed($_[0]) || $_[0]; |
61 | return $METAS{$class}; |
62 | } |
63 | |
be7677c7 |
64 | # NOTE: |
1d68af04 |
65 | # We only cache metaclasses, meaning instances of |
66 | # Class::MOP::Class. We do not cache instance of |
be7677c7 |
67 | # Class::MOP::Package or Class::MOP::Module. Mostly |
1d68af04 |
68 | # because I don't yet see a good reason to do so. |
be7677c7 |
69 | } |
70 | |
44da14be |
71 | sub _class_to_pmfile { |
72 | my $class = shift; |
73 | |
74 | my $file = $class . '.pm'; |
75 | $file =~ s{::}{/}g; |
76 | |
77 | return $file; |
78 | } |
79 | |
063ad0c5 |
80 | sub load_first_existing_class { |
f280f05c |
81 | my @classes = @_ |
a02f24cb |
82 | or return; |
5a24cf8a |
83 | |
84 | foreach my $class (@classes) { |
85 | unless ( _is_valid_class_name($class) ) { |
86 | my $display = defined($class) ? $class : 'undef'; |
87 | confess "Invalid class name ($display)"; |
88 | } |
ab5e2f48 |
89 | } |
90 | |
063ad0c5 |
91 | my $found; |
5a24cf8a |
92 | my %exceptions; |
063ad0c5 |
93 | |
a02f24cb |
94 | for my $class (@classes) { |
95 | my $file = _class_to_pmfile($class); |
5a24cf8a |
96 | |
a02f24cb |
97 | return $class if is_class_loaded($class);; |
07940968 |
98 | |
a02f24cb |
99 | return $class if try { |
100 | local $SIG{__DIE__}; |
101 | require $file; |
102 | return 1; |
103 | } |
104 | catch { |
105 | unless (/^Can't locate \Q$file\E in \@INC/) { |
106 | confess "Couldn't load class ($class) because: $_"; |
44da14be |
107 | } |
063ad0c5 |
108 | |
a02f24cb |
109 | return; |
110 | }; |
a8344505 |
111 | } |
a8344505 |
112 | |
a02f24cb |
113 | if ( @classes > 1 ) { |
114 | confess "Can't locate any of @classes in \@INC (\@INC contains: @INC)."; |
115 | } else { |
116 | confess "Can't locate " . _class_to_pmfile($classes[0]) . " in \@INC (\@INC contains: @INC)."; |
117 | } |
063ad0c5 |
118 | } |
119 | |
5a24cf8a |
120 | sub load_class { |
7716a8f9 |
121 | load_first_existing_class($_[0]); |
122 | |
674d9359 |
123 | # This is done to avoid breaking code which checked the return value. Said |
124 | # code is dumb. The return value was _always_ true, since it dies on |
125 | # failure! |
126 | return 1; |
448b6e55 |
127 | } |
128 | |
2c0fb064 |
129 | sub _is_valid_class_name { |
130 | my $class = shift; |
131 | |
132 | return 0 if ref($class); |
133 | return 0 unless defined($class); |
134 | return 0 unless length($class); |
135 | |
136 | return 1 if $class =~ /^\w+(?:::\w+)*$/; |
137 | |
138 | return 0; |
139 | } |
140 | |
aa448b16 |
141 | ## ---------------------------------------------------------------------------- |
142 | ## Setting up our environment ... |
143 | ## ---------------------------------------------------------------------------- |
1d68af04 |
144 | ## Class::MOP needs to have a few things in the global perl environment so |
aa448b16 |
145 | ## that it can operate effectively. Those things are done here. |
146 | ## ---------------------------------------------------------------------------- |
147 | |
3bf7644b |
148 | # ... nothing yet actually ;) |
8b978dd5 |
149 | |
b51af7f9 |
150 | ## ---------------------------------------------------------------------------- |
1d68af04 |
151 | ## Bootstrapping |
b51af7f9 |
152 | ## ---------------------------------------------------------------------------- |
1d68af04 |
153 | ## The code below here is to bootstrap our MOP with itself. This is also |
b51af7f9 |
154 | ## sometimes called "tying the knot". By doing this, we make it much easier |
155 | ## to extend the MOP through subclassing and such since now you can use the |
1d68af04 |
156 | ## MOP itself to extend itself. |
157 | ## |
b51af7f9 |
158 | ## Yes, I know, thats weird and insane, but it's a good thing, trust me :) |
1d68af04 |
159 | ## ---------------------------------------------------------------------------- |
727919c5 |
160 | |
1d68af04 |
161 | # We need to add in the meta-attributes here so that |
162 | # any subclass of Class::MOP::* will be able to |
c2b256bc |
163 | # inherit them using _construct_instance |
727919c5 |
164 | |
f0480c45 |
165 | ## -------------------------------------------------------- |
30bf0c82 |
166 | ## Class::MOP::Mixin::HasMethods |
727919c5 |
167 | |
30bf0c82 |
168 | Class::MOP::Mixin::HasMethods->meta->add_attribute( |
e3e651fb |
169 | Class::MOP::Attribute->new('_methods' => ( |
b880e0de |
170 | reader => { |
e3e651fb |
171 | # NOTE: |
ce2ae40f |
172 | # we just alias the original method |
1d68af04 |
173 | # rather than re-produce it here |
30bf0c82 |
174 | '_full_method_map' => \&Class::MOP::Mixin::HasMethods::_full_method_map |
b880e0de |
175 | }, |
e3e651fb |
176 | default => sub { {} } |
727919c5 |
177 | )) |
178 | ); |
179 | |
30bf0c82 |
180 | Class::MOP::Mixin::HasMethods->meta->add_attribute( |
e3e651fb |
181 | Class::MOP::Attribute->new('method_metaclass' => ( |
182 | reader => { |
56dcfc1a |
183 | # NOTE: |
ce2ae40f |
184 | # we just alias the original method |
185 | # rather than re-produce it here |
30bf0c82 |
186 | 'method_metaclass' => \&Class::MOP::Mixin::HasMethods::method_metaclass |
a5e51f0b |
187 | }, |
e3e651fb |
188 | default => 'Class::MOP::Method', |
a5e51f0b |
189 | )) |
190 | ); |
191 | |
30bf0c82 |
192 | Class::MOP::Mixin::HasMethods->meta->add_attribute( |
e3e651fb |
193 | Class::MOP::Attribute->new('wrapped_method_metaclass' => ( |
bcef1f7c |
194 | reader => { |
195 | # NOTE: |
196 | # we just alias the original method |
197 | # rather than re-produce it here |
30bf0c82 |
198 | 'wrapped_method_metaclass' => \&Class::MOP::Mixin::HasMethods::wrapped_method_metaclass |
bcef1f7c |
199 | }, |
e3e651fb |
200 | default => 'Class::MOP::Method::Wrapped', |
bcef1f7c |
201 | )) |
202 | ); |
203 | |
e3e651fb |
204 | ## -------------------------------------------------------- |
30bf0c82 |
205 | ## Class::MOP::Mixin::HasMethods |
b71bd1cd |
206 | |
30bf0c82 |
207 | Class::MOP::Mixin::HasAttributes->meta->add_attribute( |
b71bd1cd |
208 | Class::MOP::Attribute->new('attributes' => ( |
209 | reader => { |
210 | # NOTE: we need to do this in order |
211 | # for the instance meta-object to |
212 | # not fall into meta-circular death |
213 | # |
214 | # we just alias the original method |
215 | # rather than re-produce it here |
30bf0c82 |
216 | '_attribute_map' => \&Class::MOP::Mixin::HasAttributes::_attribute_map |
b71bd1cd |
217 | }, |
218 | default => sub { {} } |
219 | )) |
220 | ); |
221 | |
30bf0c82 |
222 | Class::MOP::Mixin::HasAttributes->meta->add_attribute( |
b71bd1cd |
223 | Class::MOP::Attribute->new('attribute_metaclass' => ( |
224 | reader => { |
225 | # NOTE: |
226 | # we just alias the original method |
227 | # rather than re-produce it here |
30bf0c82 |
228 | 'attribute_metaclass' => \&Class::MOP::Mixin::HasAttributes::attribute_metaclass |
b71bd1cd |
229 | }, |
230 | default => 'Class::MOP::Attribute', |
231 | )) |
232 | ); |
233 | |
234 | ## -------------------------------------------------------- |
e3e651fb |
235 | ## Class::MOP::Package |
236 | |
bcef1f7c |
237 | Class::MOP::Package->meta->add_attribute( |
e3e651fb |
238 | Class::MOP::Attribute->new('package' => ( |
b1ff395f |
239 | reader => { |
e3e651fb |
240 | # NOTE: we need to do this in order |
241 | # for the instance meta-object to |
242 | # not fall into meta-circular death |
243 | # |
b1ff395f |
244 | # we just alias the original method |
245 | # rather than re-produce it here |
e3e651fb |
246 | 'name' => \&Class::MOP::Package::name |
b1ff395f |
247 | }, |
b1ff395f |
248 | )) |
249 | ); |
250 | |
251 | Class::MOP::Package->meta->add_attribute( |
e3e651fb |
252 | Class::MOP::Attribute->new('namespace' => ( |
253 | reader => { |
b1ff395f |
254 | # NOTE: |
255 | # we just alias the original method |
256 | # rather than re-produce it here |
e3e651fb |
257 | 'namespace' => \&Class::MOP::Package::namespace |
b1ff395f |
258 | }, |
e3e651fb |
259 | init_arg => undef, |
260 | default => sub { \undef } |
b1ff395f |
261 | )) |
262 | ); |
263 | |
f0480c45 |
264 | ## -------------------------------------------------------- |
265 | ## Class::MOP::Module |
266 | |
267 | # NOTE: |
1d68af04 |
268 | # yeah this is kind of stretching things a bit, |
f0480c45 |
269 | # but truthfully the version should be an attribute |
1d68af04 |
270 | # of the Module, the weirdness comes from having to |
271 | # stick to Perl 5 convention and store it in the |
272 | # $VERSION package variable. Basically if you just |
273 | # squint at it, it will look how you want it to look. |
f0480c45 |
274 | # Either as a package variable, or as a attribute of |
275 | # the metaclass, isn't abstraction great :) |
276 | |
277 | Class::MOP::Module->meta->add_attribute( |
8683db0e |
278 | Class::MOP::Attribute->new('version' => ( |
f0480c45 |
279 | reader => { |
ce2ae40f |
280 | # NOTE: |
281 | # we just alias the original method |
1d68af04 |
282 | # rather than re-produce it here |
ce2ae40f |
283 | 'version' => \&Class::MOP::Module::version |
f0480c45 |
284 | }, |
2e877f58 |
285 | init_arg => undef, |
c4260b45 |
286 | default => sub { \undef } |
f0480c45 |
287 | )) |
288 | ); |
289 | |
290 | # NOTE: |
1d68af04 |
291 | # By following the same conventions as version here, |
292 | # we are opening up the possibility that people can |
293 | # use the $AUTHORITY in non-Class::MOP modules as |
294 | # well. |
f0480c45 |
295 | |
296 | Class::MOP::Module->meta->add_attribute( |
8683db0e |
297 | Class::MOP::Attribute->new('authority' => ( |
f0480c45 |
298 | reader => { |
ce2ae40f |
299 | # NOTE: |
300 | # we just alias the original method |
1d68af04 |
301 | # rather than re-produce it here |
ce2ae40f |
302 | 'authority' => \&Class::MOP::Module::authority |
1d68af04 |
303 | }, |
2e877f58 |
304 | init_arg => undef, |
c4260b45 |
305 | default => sub { \undef } |
f0480c45 |
306 | )) |
307 | ); |
308 | |
309 | ## -------------------------------------------------------- |
6d5355c3 |
310 | ## Class::MOP::Class |
311 | |
727919c5 |
312 | Class::MOP::Class->meta->add_attribute( |
8683db0e |
313 | Class::MOP::Attribute->new('superclasses' => ( |
c23184fc |
314 | accessor => { |
315 | # NOTE: |
316 | # we just alias the original method |
1d68af04 |
317 | # rather than re-produce it here |
c23184fc |
318 | 'superclasses' => \&Class::MOP::Class::superclasses |
319 | }, |
2e877f58 |
320 | init_arg => undef, |
c23184fc |
321 | default => sub { \undef } |
322 | )) |
323 | ); |
324 | |
325 | Class::MOP::Class->meta->add_attribute( |
8683db0e |
326 | Class::MOP::Attribute->new('instance_metaclass' => ( |
b880e0de |
327 | reader => { |
1d68af04 |
328 | # NOTE: we need to do this in order |
329 | # for the instance meta-object to |
330 | # not fall into meta-circular death |
331 | # |
ce2ae40f |
332 | # we just alias the original method |
1d68af04 |
333 | # rather than re-produce it here |
ce2ae40f |
334 | 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass |
b880e0de |
335 | }, |
1d68af04 |
336 | default => 'Class::MOP::Instance', |
2bab2be6 |
337 | )) |
338 | ); |
339 | |
44d6ea77 |
340 | Class::MOP::Class->meta->add_attribute( |
f5d08022 |
341 | Class::MOP::Attribute->new('immutable_trait' => ( |
342 | reader => { |
343 | 'immutable_trait' => \&Class::MOP::Class::immutable_trait |
344 | }, |
345 | default => "Class::MOP::Class::Immutable::Trait", |
346 | )) |
347 | ); |
348 | |
349 | Class::MOP::Class->meta->add_attribute( |
350 | Class::MOP::Attribute->new('constructor_name' => ( |
44d6ea77 |
351 | reader => { |
f5d08022 |
352 | 'constructor_name' => \&Class::MOP::Class::constructor_name, |
44d6ea77 |
353 | }, |
f5d08022 |
354 | default => "new", |
355 | )) |
356 | ); |
357 | |
358 | Class::MOP::Class->meta->add_attribute( |
359 | Class::MOP::Attribute->new('constructor_class' => ( |
44d6ea77 |
360 | reader => { |
f5d08022 |
361 | 'constructor_class' => \&Class::MOP::Class::constructor_class, |
44d6ea77 |
362 | }, |
f5d08022 |
363 | default => "Class::MOP::Method::Constructor", |
364 | )) |
365 | ); |
366 | |
367 | |
368 | Class::MOP::Class->meta->add_attribute( |
369 | Class::MOP::Attribute->new('destructor_class' => ( |
370 | reader => { |
371 | 'destructor_class' => \&Class::MOP::Class::destructor_class, |
44d6ea77 |
372 | }, |
373 | )) |
374 | ); |
375 | |
9d6dce77 |
376 | # NOTE: |
1d68af04 |
377 | # we don't actually need to tie the knot with |
378 | # Class::MOP::Class here, it is actually handled |
379 | # within Class::MOP::Class itself in the |
c2b256bc |
380 | # _construct_class_instance method. |
9d6dce77 |
381 | |
f0480c45 |
382 | ## -------------------------------------------------------- |
2611f98e |
383 | ## Class::MOP::Mixin::AttributeBase |
384 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
385 | Class::MOP::Attribute->new('name' => ( |
c23184fc |
386 | reader => { |
1d68af04 |
387 | # NOTE: we need to do this in order |
388 | # for the instance meta-object to |
389 | # not fall into meta-circular death |
390 | # |
ce2ae40f |
391 | # we just alias the original method |
1d68af04 |
392 | # rather than re-produce it here |
2611f98e |
393 | 'name' => \&Class::MOP::Mixin::AttributeBase::name |
b880e0de |
394 | } |
7b31baf4 |
395 | )) |
396 | ); |
397 | |
2611f98e |
398 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
399 | Class::MOP::Attribute->new('accessor' => ( |
2611f98e |
400 | reader => { 'accessor' => \&Class::MOP::Mixin::AttributeBase::accessor }, |
401 | predicate => { 'has_accessor' => \&Class::MOP::Mixin::AttributeBase::has_accessor }, |
7b31baf4 |
402 | )) |
403 | ); |
404 | |
2611f98e |
405 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
406 | Class::MOP::Attribute->new('reader' => ( |
2611f98e |
407 | reader => { 'reader' => \&Class::MOP::Mixin::AttributeBase::reader }, |
408 | predicate => { 'has_reader' => \&Class::MOP::Mixin::AttributeBase::has_reader }, |
7b31baf4 |
409 | )) |
410 | ); |
411 | |
2611f98e |
412 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
413 | Class::MOP::Attribute->new('initializer' => ( |
2611f98e |
414 | reader => { 'initializer' => \&Class::MOP::Mixin::AttributeBase::initializer }, |
415 | predicate => { 'has_initializer' => \&Class::MOP::Mixin::AttributeBase::has_initializer }, |
0ab65f99 |
416 | )) |
417 | ); |
418 | |
2611f98e |
419 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
d9d99689 |
420 | Class::MOP::Attribute->new('definition_context' => ( |
2611f98e |
421 | reader => { 'definition_context' => \&Class::MOP::Mixin::AttributeBase::definition_context }, |
d9d99689 |
422 | )) |
423 | ); |
424 | |
2611f98e |
425 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
426 | Class::MOP::Attribute->new('writer' => ( |
2611f98e |
427 | reader => { 'writer' => \&Class::MOP::Mixin::AttributeBase::writer }, |
428 | predicate => { 'has_writer' => \&Class::MOP::Mixin::AttributeBase::has_writer }, |
7b31baf4 |
429 | )) |
430 | ); |
431 | |
2611f98e |
432 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
433 | Class::MOP::Attribute->new('predicate' => ( |
2611f98e |
434 | reader => { 'predicate' => \&Class::MOP::Mixin::AttributeBase::predicate }, |
435 | predicate => { 'has_predicate' => \&Class::MOP::Mixin::AttributeBase::has_predicate }, |
7b31baf4 |
436 | )) |
437 | ); |
438 | |
2611f98e |
439 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
440 | Class::MOP::Attribute->new('clearer' => ( |
2611f98e |
441 | reader => { 'clearer' => \&Class::MOP::Mixin::AttributeBase::clearer }, |
442 | predicate => { 'has_clearer' => \&Class::MOP::Mixin::AttributeBase::has_clearer }, |
7d28758b |
443 | )) |
444 | ); |
445 | |
2611f98e |
446 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
447 | Class::MOP::Attribute->new('builder' => ( |
2611f98e |
448 | reader => { 'builder' => \&Class::MOP::Mixin::AttributeBase::builder }, |
449 | predicate => { 'has_builder' => \&Class::MOP::Mixin::AttributeBase::has_builder }, |
1d68af04 |
450 | )) |
451 | ); |
452 | |
2611f98e |
453 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
454 | Class::MOP::Attribute->new('init_arg' => ( |
2611f98e |
455 | reader => { 'init_arg' => \&Class::MOP::Mixin::AttributeBase::init_arg }, |
456 | predicate => { 'has_init_arg' => \&Class::MOP::Mixin::AttributeBase::has_init_arg }, |
7b31baf4 |
457 | )) |
458 | ); |
459 | |
2611f98e |
460 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
8683db0e |
461 | Class::MOP::Attribute->new('default' => ( |
7b31baf4 |
462 | # default has a custom 'reader' method ... |
2611f98e |
463 | predicate => { 'has_default' => \&Class::MOP::Mixin::AttributeBase::has_default }, |
7b31baf4 |
464 | )) |
465 | ); |
466 | |
2611f98e |
467 | Class::MOP::Mixin::AttributeBase->meta->add_attribute( |
468 | Class::MOP::Attribute->new('insertion_order' => ( |
469 | reader => { 'insertion_order' => \&Class::MOP::Mixin::AttributeBase::insertion_order }, |
470 | writer => { '_set_insertion_order' => \&Class::MOP::Mixin::AttributeBase::_set_insertion_order }, |
471 | predicate => { 'has_insertion_order' => \&Class::MOP::Mixin::AttributeBase::has_insertion_order }, |
472 | )) |
473 | ); |
474 | |
475 | ## -------------------------------------------------------- |
476 | ## Class::MOP::Attribute |
3545c727 |
477 | Class::MOP::Attribute->meta->add_attribute( |
2611f98e |
478 | Class::MOP::Attribute->new('associated_class' => ( |
479 | reader => { |
480 | # NOTE: we need to do this in order |
481 | # for the instance meta-object to |
482 | # not fall into meta-circular death |
483 | # |
484 | # we just alias the original method |
485 | # rather than re-produce it here |
486 | 'associated_class' => \&Class::MOP::Attribute::associated_class |
487 | } |
3545c727 |
488 | )) |
489 | ); |
727919c5 |
490 | |
95b67145 |
491 | Class::MOP::Attribute->meta->add_attribute( |
2611f98e |
492 | Class::MOP::Attribute->new('associated_methods' => ( |
493 | reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods }, |
494 | default => sub { [] } |
95b67145 |
495 | )) |
496 | ); |
497 | |
5659d76e |
498 | Class::MOP::Attribute->meta->add_method('clone' => sub { |
a740253a |
499 | my $self = shift; |
1d68af04 |
500 | $self->meta->clone_object($self, @_); |
727919c5 |
501 | }); |
502 | |
f0480c45 |
503 | ## -------------------------------------------------------- |
b6164407 |
504 | ## Class::MOP::Method |
b6164407 |
505 | Class::MOP::Method->meta->add_attribute( |
8683db0e |
506 | Class::MOP::Attribute->new('body' => ( |
c23184fc |
507 | reader => { 'body' => \&Class::MOP::Method::body }, |
b6164407 |
508 | )) |
509 | ); |
510 | |
4c105333 |
511 | Class::MOP::Method->meta->add_attribute( |
5e607260 |
512 | Class::MOP::Attribute->new('associated_metaclass' => ( |
5e607260 |
513 | reader => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass }, |
514 | )) |
515 | ); |
516 | |
517 | Class::MOP::Method->meta->add_attribute( |
8683db0e |
518 | Class::MOP::Attribute->new('package_name' => ( |
4c105333 |
519 | reader => { 'package_name' => \&Class::MOP::Method::package_name }, |
520 | )) |
521 | ); |
522 | |
523 | Class::MOP::Method->meta->add_attribute( |
8683db0e |
524 | Class::MOP::Attribute->new('name' => ( |
4c105333 |
525 | reader => { 'name' => \&Class::MOP::Method::name }, |
526 | )) |
527 | ); |
528 | |
2226a8b0 |
529 | Class::MOP::Method->meta->add_attribute( |
530 | Class::MOP::Attribute->new('original_method' => ( |
531 | reader => { 'original_method' => \&Class::MOP::Method::original_method }, |
532 | writer => { '_set_original_method' => \&Class::MOP::Method::_set_original_method }, |
533 | )) |
534 | ); |
535 | |
4c105333 |
536 | Class::MOP::Method->meta->add_method('clone' => sub { |
537 | my $self = shift; |
2226a8b0 |
538 | my $clone = $self->meta->clone_object($self, @_); |
539 | $clone->_set_original_method($self); |
540 | return $clone; |
4c105333 |
541 | }); |
542 | |
b6164407 |
543 | ## -------------------------------------------------------- |
544 | ## Class::MOP::Method::Wrapped |
545 | |
546 | # NOTE: |
1d68af04 |
547 | # the way this item is initialized, this |
548 | # really does not follow the standard |
549 | # practices of attributes, but we put |
b6164407 |
550 | # it here for completeness |
551 | Class::MOP::Method::Wrapped->meta->add_attribute( |
8683db0e |
552 | Class::MOP::Attribute->new('modifier_table') |
b6164407 |
553 | ); |
554 | |
555 | ## -------------------------------------------------------- |
565f0cbb |
556 | ## Class::MOP::Method::Generated |
557 | |
558 | Class::MOP::Method::Generated->meta->add_attribute( |
8683db0e |
559 | Class::MOP::Attribute->new('is_inline' => ( |
565f0cbb |
560 | reader => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline }, |
4c105333 |
561 | default => 0, |
1d68af04 |
562 | )) |
565f0cbb |
563 | ); |
564 | |
d9d99689 |
565 | Class::MOP::Method::Generated->meta->add_attribute( |
566 | Class::MOP::Attribute->new('definition_context' => ( |
567 | reader => { 'definition_context' => \&Class::MOP::Method::Generated::definition_context }, |
568 | )) |
569 | ); |
570 | |
29d4e92a |
571 | |
572 | ## -------------------------------------------------------- |
573 | ## Class::MOP::Method::Inlined |
574 | |
575 | Class::MOP::Method::Inlined->meta->add_attribute( |
576 | Class::MOP::Attribute->new('_expected_method_class' => ( |
577 | reader => { '_expected_method_class' => \&Class::MOP::Method::Inlined::_expected_method_class }, |
578 | )) |
579 | ); |
580 | |
565f0cbb |
581 | ## -------------------------------------------------------- |
d90b42a6 |
582 | ## Class::MOP::Method::Accessor |
583 | |
584 | Class::MOP::Method::Accessor->meta->add_attribute( |
8683db0e |
585 | Class::MOP::Attribute->new('attribute' => ( |
1d68af04 |
586 | reader => { |
587 | 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute |
d90b42a6 |
588 | }, |
1d68af04 |
589 | )) |
d90b42a6 |
590 | ); |
591 | |
592 | Class::MOP::Method::Accessor->meta->add_attribute( |
8683db0e |
593 | Class::MOP::Attribute->new('accessor_type' => ( |
c23184fc |
594 | reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type }, |
1d68af04 |
595 | )) |
d90b42a6 |
596 | ); |
597 | |
d90b42a6 |
598 | ## -------------------------------------------------------- |
599 | ## Class::MOP::Method::Constructor |
600 | |
601 | Class::MOP::Method::Constructor->meta->add_attribute( |
8683db0e |
602 | Class::MOP::Attribute->new('options' => ( |
1d68af04 |
603 | reader => { |
604 | 'options' => \&Class::MOP::Method::Constructor::options |
d90b42a6 |
605 | }, |
4c105333 |
606 | default => sub { +{} } |
1d68af04 |
607 | )) |
d90b42a6 |
608 | ); |
609 | |
610 | Class::MOP::Method::Constructor->meta->add_attribute( |
8683db0e |
611 | Class::MOP::Attribute->new('associated_metaclass' => ( |
e8a38403 |
612 | init_arg => "metaclass", # FIXME alias and rename |
1d68af04 |
613 | reader => { |
614 | 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass |
615 | }, |
616 | )) |
d90b42a6 |
617 | ); |
618 | |
619 | ## -------------------------------------------------------- |
86482605 |
620 | ## Class::MOP::Instance |
621 | |
622 | # NOTE: |
1d68af04 |
623 | # these don't yet do much of anything, but are just |
86482605 |
624 | # included for completeness |
625 | |
626 | Class::MOP::Instance->meta->add_attribute( |
74890687 |
627 | Class::MOP::Attribute->new('associated_metaclass', |
628 | reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass }, |
629 | ), |
86482605 |
630 | ); |
631 | |
632 | Class::MOP::Instance->meta->add_attribute( |
74890687 |
633 | Class::MOP::Attribute->new('_class_name', |
634 | init_arg => undef, |
635 | reader => { _class_name => \&Class::MOP::Instance::_class_name }, |
636 | #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway |
637 | #default => sub { $_[0]->associated_metaclass->name }, |
638 | ), |
639 | ); |
640 | |
641 | Class::MOP::Instance->meta->add_attribute( |
642 | Class::MOP::Attribute->new('attributes', |
0b5d46da |
643 | reader => { attributes => \&Class::MOP::Instance::get_all_attributes }, |
74890687 |
644 | ), |
32bfc810 |
645 | ); |
646 | |
647 | Class::MOP::Instance->meta->add_attribute( |
74890687 |
648 | Class::MOP::Attribute->new('slots', |
649 | reader => { slots => \&Class::MOP::Instance::slots }, |
650 | ), |
86482605 |
651 | ); |
652 | |
63d08a9e |
653 | Class::MOP::Instance->meta->add_attribute( |
74890687 |
654 | Class::MOP::Attribute->new('slot_hash', |
655 | reader => { slot_hash => \&Class::MOP::Instance::slot_hash }, |
656 | ), |
63d08a9e |
657 | ); |
658 | |
30229767 |
659 | require Class::MOP::Deprecated unless our $no_deprecated; |
63d08a9e |
660 | |
caa051fa |
661 | # we need the meta instance of the meta instance to be created now, in order |
662 | # for the constructor to be able to use it |
663 | Class::MOP::Instance->meta->get_meta_instance; |
664 | |
caa051fa |
665 | # pretend the add_method never happenned. it hasn't yet affected anything |
666 | undef Class::MOP::Instance->meta->{_package_cache_flag}; |
667 | |
86482605 |
668 | ## -------------------------------------------------------- |
f0480c45 |
669 | ## Now close all the Class::MOP::* classes |
4d47b77f |
670 | |
1aa13cf4 |
671 | # NOTE: we don't need to inline the the accessors this only lengthens |
672 | # the compile time of the MOP, and gives us no actual benefits. |
0b9372a2 |
673 | |
674 | $_->meta->make_immutable( |
ec9e38e5 |
675 | inline_constructor => 0, |
6c2f6b5c |
676 | constructor_name => "_new", |
45582002 |
677 | inline_accessors => 0, |
0b9372a2 |
678 | ) for qw/ |
1d68af04 |
679 | Class::MOP::Package |
680 | Class::MOP::Module |
681 | Class::MOP::Class |
682 | |
0b9372a2 |
683 | Class::MOP::Attribute |
1d68af04 |
684 | Class::MOP::Method |
685 | Class::MOP::Instance |
686 | |
687 | Class::MOP::Object |
0b9372a2 |
688 | |
565f0cbb |
689 | Class::MOP::Method::Generated |
29d4e92a |
690 | Class::MOP::Method::Inlined |
1d68af04 |
691 | |
ba38bf08 |
692 | Class::MOP::Method::Accessor |
1d68af04 |
693 | Class::MOP::Method::Constructor |
694 | Class::MOP::Method::Wrapped |
0b9372a2 |
695 | /; |
b6164407 |
696 | |
e3e651fb |
697 | $_->meta->make_immutable( |
698 | inline_constructor => 0, |
699 | constructor_name => undef, |
700 | inline_accessors => 0, |
701 | ) for qw/ |
2611f98e |
702 | Class::MOP::Mixin::AttributeBase |
30bf0c82 |
703 | Class::MOP::Mixin::HasAttributes |
704 | Class::MOP::Mixin::HasMethods |
e3e651fb |
705 | /; |
706 | |
94b19069 |
707 | 1; |
708 | |
709 | __END__ |
710 | |
711 | =pod |
712 | |
1d68af04 |
713 | =head1 NAME |
94b19069 |
714 | |
715 | Class::MOP - A Meta Object Protocol for Perl 5 |
716 | |
5b60bf98 |
717 | =head1 DESCRIPTION |
94b19069 |
718 | |
127d39a7 |
719 | This module is a fully functioning meta object protocol for the |
1d68af04 |
720 | Perl 5 object system. It makes no attempt to change the behavior or |
721 | characteristics of the Perl 5 object system, only to create a |
27e31eaf |
722 | protocol for its manipulation and introspection. |
94b19069 |
723 | |
828ecf13 |
724 | That said, it does attempt to create the tools for building a rich set |
725 | of extensions to the Perl 5 object system. Every attempt has been made |
726 | to abide by the spirit of the Perl 5 object system that we all know |
727 | and love. |
94b19069 |
728 | |
828ecf13 |
729 | This documentation is sparse on conceptual details. We suggest looking |
730 | at the items listed in the L<SEE ALSO> section for more |
731 | information. In particular the book "The Art of the Meta Object |
732 | Protocol" was very influential in the development of this system. |
40483095 |
733 | |
bfe4d0fc |
734 | =head2 What is a Meta Object Protocol? |
735 | |
1d68af04 |
736 | A meta object protocol is an API to an object system. |
bfe4d0fc |
737 | |
828ecf13 |
738 | To be more specific, it abstracts the components of an object system |
739 | (classes, object, methods, object attributes, etc.). These |
740 | abstractions can then be used to inspect and manipulate the object |
741 | system which they describe. |
bfe4d0fc |
742 | |
1d68af04 |
743 | It can be said that there are two MOPs for any object system; the |
828ecf13 |
744 | implicit MOP and the explicit MOP. The implicit MOP handles things |
1d68af04 |
745 | like method dispatch or inheritance, which happen automatically as |
746 | part of how the object system works. The explicit MOP typically |
747 | handles the introspection/reflection features of the object system. |
bfe4d0fc |
748 | |
828ecf13 |
749 | All object systems have implicit MOPs. Without one, they would not |
5b60bf98 |
750 | work. Explicit MOPs are much less common, and depending on the |
751 | language can vary from restrictive (Reflection in Java or C#) to wide |
752 | open (CLOS is a perfect example). |
e16da3e6 |
753 | |
828ecf13 |
754 | =head2 Yet Another Class Builder! Why? |
755 | |
756 | This is B<not> a class builder so much as a I<class builder |
757 | B<builder>>. The intent is that an end user will not use this module |
758 | directly, but instead this module is used by module authors to build |
759 | extensions and features onto the Perl 5 object system. |
760 | |
761 | This system is used by L<Moose>, which supplies a powerful class |
762 | builder system built entirely on top of C<Class::MOP>. |
e16da3e6 |
763 | |
94b19069 |
764 | =head2 Who is this module for? |
765 | |
828ecf13 |
766 | This module is for anyone who has ever created or wanted to create a |
767 | module for the Class:: namespace. The tools which this module provides |
768 | make doing complex Perl 5 wizardry simpler, by removing such barriers |
769 | as the need to hack symbol tables, or understand the fine details of |
770 | method dispatch. |
94b19069 |
771 | |
bfe4d0fc |
772 | =head2 What changes do I have to make to use this module? |
773 | |
828ecf13 |
774 | This module was designed to be as unintrusive as possible. Many of its |
5b60bf98 |
775 | features are accessible without B<any> change to your existing |
828ecf13 |
776 | code. It is meant to be a compliment to your existing code and not an |
777 | intrusion on your code base. Unlike many other B<Class::> modules, |
778 | this module B<does not> require you subclass it, or even that you |
779 | C<use> it in within your module's package. |
bfe4d0fc |
780 | |
1d68af04 |
781 | The only features which requires additions to your code are the |
2eb717d5 |
782 | attribute handling and instance construction features, and these are |
1d68af04 |
783 | both completely optional features. The only reason for this is because |
784 | Perl 5's object system does not actually have these features built |
2eb717d5 |
785 | in. More information about this feature can be found below. |
bfe4d0fc |
786 | |
828ecf13 |
787 | =head2 About Performance |
788 | |
5b60bf98 |
789 | It is a common misconception that explicit MOPs are a performance hit. |
828ecf13 |
790 | This is not a universal truth, it is a side-effect of some specific |
791 | implementations. For instance, using Java reflection is slow because |
792 | the JVM cannot take advantage of any compiler optimizations, and the |
793 | JVM has to deal with much more runtime type information as well. |
bfe4d0fc |
794 | |
828ecf13 |
795 | Reflection in C# is marginally better as it was designed into the |
796 | language and runtime (the CLR). In contrast, CLOS (the Common Lisp |
797 | Object System) was built to support an explicit MOP, and so |
798 | performance is tuned for it. |
1d68af04 |
799 | |
828ecf13 |
800 | This library in particular does its absolute best to avoid putting |
1d68af04 |
801 | B<any> drain at all upon your code's performance. In fact, by itself |
828ecf13 |
802 | it does nothing to affect your existing code. So you only pay for what |
803 | you actually use. |
bfe4d0fc |
804 | |
550d56db |
805 | =head2 About Metaclass compatibility |
806 | |
1d68af04 |
807 | This module makes sure that all metaclasses created are both upwards |
808 | and downwards compatible. The topic of metaclass compatibility is |
809 | highly esoteric and is something only encountered when doing deep and |
810 | involved metaclass hacking. There are two basic kinds of metaclass |
811 | incompatibility; upwards and downwards. |
550d56db |
812 | |
1d68af04 |
813 | Upwards metaclass compatibility means that the metaclass of a |
814 | given class is either the same as (or a subclass of) all of the |
550d56db |
815 | class's ancestors. |
816 | |
1d68af04 |
817 | Downward metaclass compatibility means that the metaclasses of a |
828ecf13 |
818 | given class's ancestors are all either the same as (or a subclass |
550d56db |
819 | of) that metaclass. |
820 | |
1d68af04 |
821 | Here is a diagram showing a set of two classes (C<A> and C<B>) and |
822 | two metaclasses (C<Meta::A> and C<Meta::B>) which have correct |
550d56db |
823 | metaclass compatibility both upwards and downwards. |
824 | |
825 | +---------+ +---------+ |
826 | | Meta::A |<----| Meta::B | <....... (instance of ) |
1d68af04 |
827 | +---------+ +---------+ <------- (inherits from) |
550d56db |
828 | ^ ^ |
829 | : : |
830 | +---------+ +---------+ |
831 | | A |<----| B | |
832 | +---------+ +---------+ |
833 | |
1d68af04 |
834 | As I said this is a highly esoteric topic and one you will only run |
828ecf13 |
835 | into if you do a lot of subclassing of L<Class::MOP::Class>. If you |
836 | are interested in why this is an issue see the paper I<Uniform and |
837 | safe metaclass composition> linked to in the L<SEE ALSO> section of |
838 | this document. |
550d56db |
839 | |
aa448b16 |
840 | =head2 Using custom metaclasses |
841 | |
828ecf13 |
842 | Always use the L<metaclass> pragma when using a custom metaclass, this |
5b60bf98 |
843 | will ensure the proper initialization order and not accidentally |
844 | create an incorrect type of metaclass for you. This is a very rare |
845 | problem, and one which can only occur if you are doing deep metaclass |
aa448b16 |
846 | programming. So in other words, don't worry about it. |
847 | |
828ecf13 |
848 | Note that if you're using L<Moose> we encourage you to I<not> use |
849 | L<metaclass> pragma, and instead use L<Moose::Util::MetaRole> to apply |
850 | roles to a class's metaclasses. This topic is covered at length in |
851 | various L<Moose::Cookbook> recipes. |
852 | |
94b19069 |
853 | =head1 PROTOCOLS |
854 | |
828ecf13 |
855 | The meta-object protocol is divided into 4 main sub-protocols: |
94b19069 |
856 | |
828ecf13 |
857 | =head2 The Class protocol |
94b19069 |
858 | |
1d68af04 |
859 | This provides a means of manipulating and introspecting a Perl 5 |
828ecf13 |
860 | class. It handles symbol table hacking for you, and provides a rich |
861 | set of methods that go beyond simple package introspection. |
94b19069 |
862 | |
552e3d24 |
863 | See L<Class::MOP::Class> for more details. |
864 | |
828ecf13 |
865 | =head2 The Attribute protocol |
94b19069 |
866 | |
828ecf13 |
867 | This provides a consistent representation for an attribute of a Perl 5 |
868 | class. Since there are so many ways to create and handle attributes in |
869 | Perl 5 OO, the Attribute protocol provide as much of a unified |
870 | approach as possible. Of course, you are always free to extend this |
871 | protocol by subclassing the appropriate classes. |
94b19069 |
872 | |
552e3d24 |
873 | See L<Class::MOP::Attribute> for more details. |
874 | |
828ecf13 |
875 | =head2 The Method protocol |
94b19069 |
876 | |
828ecf13 |
877 | This provides a means of manipulating and introspecting methods in the |
878 | Perl 5 object system. As with attributes, there are many ways to |
1d68af04 |
879 | approach this topic, so we try to keep it pretty basic, while still |
94b19069 |
880 | making it possible to extend the system in many ways. |
881 | |
552e3d24 |
882 | See L<Class::MOP::Method> for more details. |
94b19069 |
883 | |
828ecf13 |
884 | =head2 The Instance protocol |
127d39a7 |
885 | |
828ecf13 |
886 | This provides a layer of abstraction for creating object instances. |
887 | Since the other layers use this protocol, it is relatively easy to |
888 | change the type of your instances from the default hash reference to |
889 | some other type of reference. Several examples are provided in the |
890 | F<examples/> directory included in this distribution. |
127d39a7 |
891 | |
892 | See L<Class::MOP::Instance> for more details. |
893 | |
be7677c7 |
894 | =head1 FUNCTIONS |
895 | |
828ecf13 |
896 | Note that this module does not export any constants or functions. |
897 | |
c1d5345a |
898 | =head2 Constants |
899 | |
900 | =over 4 |
901 | |
828ecf13 |
902 | =item I<Class::MOP::IS_RUNNING_ON_5_10> |
c1d5345a |
903 | |
828ecf13 |
904 | We set this constant depending on what version perl we are on, this |
905 | allows us to take advantage of new 5.10 features and stay backwards |
5b60bf98 |
906 | compatible. |
c1d5345a |
907 | |
908 | =back |
909 | |
448b6e55 |
910 | =head2 Utility functions |
911 | |
bd07fbdb |
912 | Note that these are all called as B<functions, not methods>. |
081a927b |
913 | |
448b6e55 |
914 | =over 4 |
915 | |
828ecf13 |
916 | =item B<Class::MOP::load_class($class_name)> |
448b6e55 |
917 | |
9d441c23 |
918 | This will load the specified C<$class_name>, if it is not already |
919 | loaded (as reported by C<is_class_loaded>). This function can be used |
828ecf13 |
920 | in place of tricks like C<eval "use $module"> or using C<require> |
7716a8f9 |
921 | unconditionally. |
448b6e55 |
922 | |
674d9359 |
923 | If the module cannot be loaded, an exception is thrown. |
924 | |
78527c84 |
925 | For historical reasons, this function explicitly returns a true value. |
674d9359 |
926 | |
828ecf13 |
927 | =item B<Class::MOP::is_class_loaded($class_name)> |
448b6e55 |
928 | |
15ab5451 |
929 | Returns a boolean indicating whether or not C<$class_name> has been |
930 | loaded. |
448b6e55 |
931 | |
828ecf13 |
932 | This does a basic check of the symbol table to try and determine as |
933 | best it can if the C<$class_name> is loaded, it is probably correct |
934 | about 99% of the time, but it can be fooled into reporting false |
9d441c23 |
935 | positives. In particular, loading any of the core L<IO> modules will |
936 | cause most of the rest of the core L<IO> modules to falsely report |
937 | having been loaded, due to the way the base L<IO> module works. |
448b6e55 |
938 | |
cdac22cc |
939 | =item B<Class::MOP::get_code_info($code)> |
940 | |
941 | This function returns two values, the name of the package the C<$code> |
942 | is from and the name of the C<$code> itself. This is used by several |
5b60bf98 |
943 | elements of the MOP to determine where a given C<$code> reference is |
cdac22cc |
944 | from. |
945 | |
44ba77a3 |
946 | =item B<Class::MOP::class_of($instance_or_class_name)> |
947 | |
3fa5b3f9 |
948 | This will return the metaclass of the given instance or class name. If the |
949 | class lacks a metaclass, no metaclass will be initialized, and C<undef> will be |
950 | returned. |
44ba77a3 |
951 | |
828ecf13 |
952 | =item B<Class::MOP::check_package_cache_flag($pkg)> |
e0e4674a |
953 | |
bd07fbdb |
954 | B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!> |
955 | |
828ecf13 |
956 | This will return an integer that is managed by L<Class::MOP::Class> to |
957 | determine if a module's symbol table has been altered. |
127d39a7 |
958 | |
828ecf13 |
959 | In Perl 5.10 or greater, this flag is package specific. However in |
960 | versions prior to 5.10, this will use the C<PL_sub_generation> |
961 | variable which is not package specific. |
127d39a7 |
962 | |
828ecf13 |
963 | =item B<Class::MOP::load_first_existing_class(@class_names)> |
063ad0c5 |
964 | |
965 | B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!> |
966 | |
967 | Given a list of class names, this function will attempt to load each |
968 | one in turn. |
969 | |
828ecf13 |
970 | If it finds a class it can load, it will return that class' name. If |
971 | none of the classes can be loaded, it will throw an exception. |
063ad0c5 |
972 | |
448b6e55 |
973 | =back |
974 | |
975 | =head2 Metaclass cache functions |
976 | |
6c842677 |
977 | Class::MOP holds a cache of metaclasses. The following are functions |
1d68af04 |
978 | (B<not methods>) which can be used to access that cache. It is not |
6c842677 |
979 | recommended that you mess with these. Bad things could happen, but if |
980 | you are brave and willing to risk it: go for it! |
be7677c7 |
981 | |
982 | =over 4 |
983 | |
828ecf13 |
984 | =item B<Class::MOP::get_all_metaclasses> |
be7677c7 |
985 | |
6c842677 |
986 | This will return a hash of all the metaclass instances that have |
828ecf13 |
987 | been cached by L<Class::MOP::Class>, keyed by the package name. |
b9d9fc0b |
988 | |
828ecf13 |
989 | =item B<Class::MOP::get_all_metaclass_instances> |
be7677c7 |
990 | |
6c842677 |
991 | This will return a list of all the metaclass instances that have |
828ecf13 |
992 | been cached by L<Class::MOP::Class>. |
b9d9fc0b |
993 | |
828ecf13 |
994 | =item B<Class::MOP::get_all_metaclass_names> |
be7677c7 |
995 | |
6c842677 |
996 | This will return a list of all the metaclass names that have |
828ecf13 |
997 | been cached by L<Class::MOP::Class>. |
b9d9fc0b |
998 | |
828ecf13 |
999 | =item B<Class::MOP::get_metaclass_by_name($name)> |
be7677c7 |
1000 | |
828ecf13 |
1001 | This will return a cached L<Class::MOP::Class> instance, or nothing |
6c842677 |
1002 | if no metaclass exists with that C<$name>. |
127d39a7 |
1003 | |
828ecf13 |
1004 | =item B<Class::MOP::store_metaclass_by_name($name, $meta)> |
be7677c7 |
1005 | |
127d39a7 |
1006 | This will store a metaclass in the cache at the supplied C<$key>. |
1007 | |
828ecf13 |
1008 | =item B<Class::MOP::weaken_metaclass($name)> |
be7677c7 |
1009 | |
6c842677 |
1010 | In rare cases (e.g. anonymous metaclasses) it is desirable to |
1011 | store a weakened reference in the metaclass cache. This |
1012 | function will weaken the reference to the metaclass stored |
1013 | in C<$name>. |
127d39a7 |
1014 | |
828ecf13 |
1015 | =item B<Class::MOP::does_metaclass_exist($name)> |
be7677c7 |
1016 | |
828ecf13 |
1017 | This will return true of there exists a metaclass stored in the |
6c842677 |
1018 | C<$name> key, and return false otherwise. |
127d39a7 |
1019 | |
828ecf13 |
1020 | =item B<Class::MOP::remove_metaclass_by_name($name)> |
be7677c7 |
1021 | |
6c842677 |
1022 | This will remove the metaclass stored in the C<$name> key. |
127d39a7 |
1023 | |
be7677c7 |
1024 | =back |
1025 | |
552e3d24 |
1026 | =head1 SEE ALSO |
8b978dd5 |
1027 | |
552e3d24 |
1028 | =head2 Books |
8b978dd5 |
1029 | |
1d68af04 |
1030 | There are very few books out on Meta Object Protocols and Metaclasses |
1031 | because it is such an esoteric topic. The following books are really |
1032 | the only ones I have found. If you know of any more, B<I<please>> |
a2e85e6c |
1033 | email me and let me know, I would love to hear about them. |
1034 | |
8b978dd5 |
1035 | =over 4 |
1036 | |
15ab5451 |
1037 | =item I<The Art of the Meta Object Protocol> |
8b978dd5 |
1038 | |
15ab5451 |
1039 | =item I<Advances in Object-Oriented Metalevel Architecture and Reflection> |
8b978dd5 |
1040 | |
15ab5451 |
1041 | =item I<Putting MetaClasses to Work> |
b51af7f9 |
1042 | |
15ab5451 |
1043 | =item I<Smalltalk: The Language> |
a2e85e6c |
1044 | |
94b19069 |
1045 | =back |
1046 | |
550d56db |
1047 | =head2 Papers |
1048 | |
1049 | =over 4 |
1050 | |
15ab5451 |
1051 | =item "Uniform and safe metaclass composition" |
550d56db |
1052 | |
1d68af04 |
1053 | An excellent paper by the people who brought us the original Traits paper. |
1054 | This paper is on how Traits can be used to do safe metaclass composition, |
1055 | and offers an excellent introduction section which delves into the topic of |
550d56db |
1056 | metaclass compatibility. |
1057 | |
1058 | L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf> |
1059 | |
15ab5451 |
1060 | =item "Safe Metaclass Programming" |
550d56db |
1061 | |
1d68af04 |
1062 | This paper seems to precede the above paper, and propose a mix-in based |
1063 | approach as opposed to the Traits based approach. Both papers have similar |
1064 | information on the metaclass compatibility problem space. |
550d56db |
1065 | |
1066 | L<http://citeseer.ist.psu.edu/37617.html> |
1067 | |
1068 | =back |
1069 | |
552e3d24 |
1070 | =head2 Prior Art |
8b978dd5 |
1071 | |
1072 | =over 4 |
1073 | |
7184ca14 |
1074 | =item The Perl 6 MetaModel work in the Pugs project |
8b978dd5 |
1075 | |
1076 | =over 4 |
1077 | |
02615ef0 |
1078 | =item L<http://svn.openfoundry.org/pugs/misc/Perl-MetaModel/> |
8b978dd5 |
1079 | |
552e3d24 |
1080 | =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace> |
8b978dd5 |
1081 | |
1082 | =back |
1083 | |
94b19069 |
1084 | =back |
1085 | |
1d68af04 |
1086 | =head2 Articles |
f8dfcfb7 |
1087 | |
1088 | =over 4 |
1089 | |
1d68af04 |
1090 | =item CPAN Module Review of Class::MOP |
f8dfcfb7 |
1091 | |
1092 | L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html> |
1093 | |
1094 | =back |
1095 | |
a2e85e6c |
1096 | =head1 SIMILAR MODULES |
1097 | |
1d68af04 |
1098 | As I have said above, this module is a class-builder-builder, so it is |
1099 | not the same thing as modules like L<Class::Accessor> and |
1100 | L<Class::MethodMaker>. That being said there are very few modules on CPAN |
1101 | with similar goals to this module. The one I have found which is most |
1102 | like this module is L<Class::Meta>, although it's philosophy and the MOP it |
1103 | creates are very different from this modules. |
94b19069 |
1104 | |
a2e85e6c |
1105 | =head1 BUGS |
1106 | |
1d68af04 |
1107 | All complex software has bugs lurking in it, and this module is no |
53702399 |
1108 | exception. |
1109 | |
1110 | Please report any bugs to C<bug-class-mop@rt.cpan.org>, or through the |
1111 | web interface at L<http://rt.cpan.org>. |
1112 | |
1113 | You can also discuss feature requests or possible bugs on the Moose |
1114 | mailing list (moose@perl.org) or on IRC at |
1115 | L<irc://irc.perl.org/#moose>. |
a2e85e6c |
1116 | |
1117 | =head1 ACKNOWLEDGEMENTS |
1118 | |
1119 | =over 4 |
1120 | |
b9d9fc0b |
1121 | =item Rob Kinyon |
a2e85e6c |
1122 | |
1d68af04 |
1123 | Thanks to Rob for actually getting the development of this module kick-started. |
a2e85e6c |
1124 | |
1125 | =back |
1126 | |
1a09d9cc |
1127 | =head1 AUTHORS |
94b19069 |
1128 | |
a2e85e6c |
1129 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
552e3d24 |
1130 | |
9c8cda90 |
1131 | B<with contributions from:> |
1132 | |
1133 | Brandon (blblack) Black |
1134 | |
4f116037 |
1135 | Florian (rafl) Ragwitz |
1136 | |
9c8cda90 |
1137 | Guillermo (groditi) Roditi |
1138 | |
53702399 |
1139 | Dave (autarch) Rolsky |
1140 | |
9195ddff |
1141 | Matt (mst) Trout |
1142 | |
9c8cda90 |
1143 | Rob (robkinyon) Kinyon |
1144 | |
1145 | Yuval (nothingmuch) Kogman |
1a09d9cc |
1146 | |
f430cfa4 |
1147 | Scott (konobi) McWhirter |
1148 | |
20f5ccef |
1149 | Dylan Hardison |
1150 | |
94b19069 |
1151 | =head1 COPYRIGHT AND LICENSE |
1152 | |
070bb6c9 |
1153 | Copyright 2006-2009 by Infinity Interactive, Inc. |
94b19069 |
1154 | |
1155 | L<http://www.iinteractive.com> |
1156 | |
1157 | This library is free software; you can redistribute it and/or modify |
1d68af04 |
1158 | it under the same terms as Perl itself. |
94b19069 |
1159 | |
1160 | =cut |