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