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