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