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