bump version to 0.83
[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
e00524a0 34our $VERSION = '0.83';
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' => (
44d6ea77 371 reader => {
f5d08022 372 'constructor_class' => \&Class::MOP::Class::constructor_class,
44d6ea77 373 },
f5d08022 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
29d4e92a 573
574## --------------------------------------------------------
575## Class::MOP::Method::Inlined
576
577Class::MOP::Method::Inlined->meta->add_attribute(
578 Class::MOP::Attribute->new('_expected_method_class' => (
579 reader => { '_expected_method_class' => \&Class::MOP::Method::Inlined::_expected_method_class },
580 ))
581);
582
565f0cbb 583## --------------------------------------------------------
d90b42a6 584## Class::MOP::Method::Accessor
585
586Class::MOP::Method::Accessor->meta->add_attribute(
8683db0e 587 Class::MOP::Attribute->new('attribute' => (
1d68af04 588 reader => {
589 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
d90b42a6 590 },
1d68af04 591 ))
d90b42a6 592);
593
594Class::MOP::Method::Accessor->meta->add_attribute(
8683db0e 595 Class::MOP::Attribute->new('accessor_type' => (
c23184fc 596 reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
1d68af04 597 ))
d90b42a6 598);
599
d90b42a6 600## --------------------------------------------------------
601## Class::MOP::Method::Constructor
602
603Class::MOP::Method::Constructor->meta->add_attribute(
8683db0e 604 Class::MOP::Attribute->new('options' => (
1d68af04 605 reader => {
606 'options' => \&Class::MOP::Method::Constructor::options
d90b42a6 607 },
4c105333 608 default => sub { +{} }
1d68af04 609 ))
d90b42a6 610);
611
612Class::MOP::Method::Constructor->meta->add_attribute(
8683db0e 613 Class::MOP::Attribute->new('associated_metaclass' => (
e8a38403 614 init_arg => "metaclass", # FIXME alias and rename
1d68af04 615 reader => {
616 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
617 },
618 ))
d90b42a6 619);
620
621## --------------------------------------------------------
86482605 622## Class::MOP::Instance
623
624# NOTE:
1d68af04 625# these don't yet do much of anything, but are just
86482605 626# included for completeness
627
628Class::MOP::Instance->meta->add_attribute(
74890687 629 Class::MOP::Attribute->new('associated_metaclass',
630 reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
631 ),
86482605 632);
633
634Class::MOP::Instance->meta->add_attribute(
74890687 635 Class::MOP::Attribute->new('_class_name',
636 init_arg => undef,
637 reader => { _class_name => \&Class::MOP::Instance::_class_name },
638 #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway
639 #default => sub { $_[0]->associated_metaclass->name },
640 ),
641);
642
643Class::MOP::Instance->meta->add_attribute(
644 Class::MOP::Attribute->new('attributes',
0b5d46da 645 reader => { attributes => \&Class::MOP::Instance::get_all_attributes },
74890687 646 ),
32bfc810 647);
648
649Class::MOP::Instance->meta->add_attribute(
74890687 650 Class::MOP::Attribute->new('slots',
651 reader => { slots => \&Class::MOP::Instance::slots },
652 ),
86482605 653);
654
63d08a9e 655Class::MOP::Instance->meta->add_attribute(
74890687 656 Class::MOP::Attribute->new('slot_hash',
657 reader => { slot_hash => \&Class::MOP::Instance::slot_hash },
658 ),
63d08a9e 659);
660
661
caa051fa 662# we need the meta instance of the meta instance to be created now, in order
663# for the constructor to be able to use it
664Class::MOP::Instance->meta->get_meta_instance;
665
caa051fa 666# pretend the add_method never happenned. it hasn't yet affected anything
667undef Class::MOP::Instance->meta->{_package_cache_flag};
668
86482605 669## --------------------------------------------------------
f0480c45 670## Now close all the Class::MOP::* classes
4d47b77f 671
1aa13cf4 672# NOTE: we don't need to inline the the accessors this only lengthens
673# the compile time of the MOP, and gives us no actual benefits.
0b9372a2 674
a7b17d6f 675# this is just nitpicking to ensure Class::MOP::Class->meta == ->meta->meta
e1c7a1b7 676Class::MOP::Class->meta->_immutable_metaclass;
a7b17d6f 677$Class::MOP::Class::immutable_metaclass_cache{"Class::MOP::Class"}{"Class::MOP::Class::Immutable::Trait"} = Class::MOP::Class::Immutable::Class::MOP::Class->meta;
678
0b9372a2 679$_->meta->make_immutable(
6c2f6b5c 680 inline_constructor => 1,
681 replace_constructor => 1,
682 constructor_name => "_new",
45582002 683 inline_accessors => 0,
0b9372a2 684) for qw/
1d68af04 685 Class::MOP::Package
686 Class::MOP::Module
687 Class::MOP::Class
f5d08022 688 Class::MOP::Class::Immutable::Trait
a7b17d6f 689 Class::MOP::Class::Immutable::Class::MOP::Class
1d68af04 690
0b9372a2 691 Class::MOP::Attribute
1d68af04 692 Class::MOP::Method
693 Class::MOP::Instance
694
695 Class::MOP::Object
0b9372a2 696
565f0cbb 697 Class::MOP::Method::Generated
29d4e92a 698 Class::MOP::Method::Inlined
1d68af04 699
ba38bf08 700 Class::MOP::Method::Accessor
1d68af04 701 Class::MOP::Method::Constructor
702 Class::MOP::Method::Wrapped
0b9372a2 703/;
b6164407 704
94b19069 7051;
706
707__END__
708
709=pod
710
1d68af04 711=head1 NAME
94b19069 712
713Class::MOP - A Meta Object Protocol for Perl 5
714
5b60bf98 715=head1 DESCRIPTION
94b19069 716
127d39a7 717This module is a fully functioning meta object protocol for the
1d68af04 718Perl 5 object system. It makes no attempt to change the behavior or
719characteristics of the Perl 5 object system, only to create a
27e31eaf 720protocol for its manipulation and introspection.
94b19069 721
828ecf13 722That said, it does attempt to create the tools for building a rich set
723of extensions to the Perl 5 object system. Every attempt has been made
724to abide by the spirit of the Perl 5 object system that we all know
725and love.
94b19069 726
828ecf13 727This documentation is sparse on conceptual details. We suggest looking
728at the items listed in the L<SEE ALSO> section for more
729information. In particular the book "The Art of the Meta Object
730Protocol" was very influential in the development of this system.
40483095 731
bfe4d0fc 732=head2 What is a Meta Object Protocol?
733
1d68af04 734A meta object protocol is an API to an object system.
bfe4d0fc 735
828ecf13 736To be more specific, it abstracts the components of an object system
737(classes, object, methods, object attributes, etc.). These
738abstractions can then be used to inspect and manipulate the object
739system which they describe.
bfe4d0fc 740
1d68af04 741It can be said that there are two MOPs for any object system; the
828ecf13 742implicit MOP and the explicit MOP. The implicit MOP handles things
1d68af04 743like method dispatch or inheritance, which happen automatically as
744part of how the object system works. The explicit MOP typically
745handles the introspection/reflection features of the object system.
bfe4d0fc 746
828ecf13 747All object systems have implicit MOPs. Without one, they would not
5b60bf98 748work. Explicit MOPs are much less common, and depending on the
749language can vary from restrictive (Reflection in Java or C#) to wide
750open (CLOS is a perfect example).
e16da3e6 751
828ecf13 752=head2 Yet Another Class Builder! Why?
753
754This is B<not> a class builder so much as a I<class builder
755B<builder>>. The intent is that an end user will not use this module
756directly, but instead this module is used by module authors to build
757extensions and features onto the Perl 5 object system.
758
759This system is used by L<Moose>, which supplies a powerful class
760builder system built entirely on top of C<Class::MOP>.
e16da3e6 761
94b19069 762=head2 Who is this module for?
763
828ecf13 764This module is for anyone who has ever created or wanted to create a
765module for the Class:: namespace. The tools which this module provides
766make doing complex Perl 5 wizardry simpler, by removing such barriers
767as the need to hack symbol tables, or understand the fine details of
768method dispatch.
94b19069 769
bfe4d0fc 770=head2 What changes do I have to make to use this module?
771
828ecf13 772This module was designed to be as unintrusive as possible. Many of its
5b60bf98 773features are accessible without B<any> change to your existing
828ecf13 774code. It is meant to be a compliment to your existing code and not an
775intrusion on your code base. Unlike many other B<Class::> modules,
776this module B<does not> require you subclass it, or even that you
777C<use> it in within your module's package.
bfe4d0fc 778
1d68af04 779The only features which requires additions to your code are the
2eb717d5 780attribute handling and instance construction features, and these are
1d68af04 781both completely optional features. The only reason for this is because
782Perl 5's object system does not actually have these features built
2eb717d5 783in. More information about this feature can be found below.
bfe4d0fc 784
828ecf13 785=head2 About Performance
786
5b60bf98 787It is a common misconception that explicit MOPs are a performance hit.
828ecf13 788This is not a universal truth, it is a side-effect of some specific
789implementations. For instance, using Java reflection is slow because
790the JVM cannot take advantage of any compiler optimizations, and the
791JVM has to deal with much more runtime type information as well.
bfe4d0fc 792
828ecf13 793Reflection in C# is marginally better as it was designed into the
794language and runtime (the CLR). In contrast, CLOS (the Common Lisp
795Object System) was built to support an explicit MOP, and so
796performance is tuned for it.
1d68af04 797
828ecf13 798This library in particular does its absolute best to avoid putting
1d68af04 799B<any> drain at all upon your code's performance. In fact, by itself
828ecf13 800it does nothing to affect your existing code. So you only pay for what
801you actually use.
bfe4d0fc 802
550d56db 803=head2 About Metaclass compatibility
804
1d68af04 805This module makes sure that all metaclasses created are both upwards
806and downwards compatible. The topic of metaclass compatibility is
807highly esoteric and is something only encountered when doing deep and
808involved metaclass hacking. There are two basic kinds of metaclass
809incompatibility; upwards and downwards.
550d56db 810
1d68af04 811Upwards metaclass compatibility means that the metaclass of a
812given class is either the same as (or a subclass of) all of the
550d56db 813class's ancestors.
814
1d68af04 815Downward metaclass compatibility means that the metaclasses of a
828ecf13 816given class's ancestors are all either the same as (or a subclass
550d56db 817of) that metaclass.
818
1d68af04 819Here is a diagram showing a set of two classes (C<A> and C<B>) and
820two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
550d56db 821metaclass compatibility both upwards and downwards.
822
823 +---------+ +---------+
824 | Meta::A |<----| Meta::B | <....... (instance of )
1d68af04 825 +---------+ +---------+ <------- (inherits from)
550d56db 826 ^ ^
827 : :
828 +---------+ +---------+
829 | A |<----| B |
830 +---------+ +---------+
831
1d68af04 832As I said this is a highly esoteric topic and one you will only run
828ecf13 833into if you do a lot of subclassing of L<Class::MOP::Class>. If you
834are interested in why this is an issue see the paper I<Uniform and
835safe metaclass composition> linked to in the L<SEE ALSO> section of
836this document.
550d56db 837
aa448b16 838=head2 Using custom metaclasses
839
828ecf13 840Always use the L<metaclass> pragma when using a custom metaclass, this
5b60bf98 841will ensure the proper initialization order and not accidentally
842create an incorrect type of metaclass for you. This is a very rare
843problem, and one which can only occur if you are doing deep metaclass
aa448b16 844programming. So in other words, don't worry about it.
845
828ecf13 846Note that if you're using L<Moose> we encourage you to I<not> use
847L<metaclass> pragma, and instead use L<Moose::Util::MetaRole> to apply
848roles to a class's metaclasses. This topic is covered at length in
849various L<Moose::Cookbook> recipes.
850
94b19069 851=head1 PROTOCOLS
852
828ecf13 853The meta-object protocol is divided into 4 main sub-protocols:
94b19069 854
828ecf13 855=head2 The Class protocol
94b19069 856
1d68af04 857This provides a means of manipulating and introspecting a Perl 5
828ecf13 858class. It handles symbol table hacking for you, and provides a rich
859set of methods that go beyond simple package introspection.
94b19069 860
552e3d24 861See L<Class::MOP::Class> for more details.
862
828ecf13 863=head2 The Attribute protocol
94b19069 864
828ecf13 865This provides a consistent representation for an attribute of a Perl 5
866class. Since there are so many ways to create and handle attributes in
867Perl 5 OO, the Attribute protocol provide as much of a unified
868approach as possible. Of course, you are always free to extend this
869protocol by subclassing the appropriate classes.
94b19069 870
552e3d24 871See L<Class::MOP::Attribute> for more details.
872
828ecf13 873=head2 The Method protocol
94b19069 874
828ecf13 875This provides a means of manipulating and introspecting methods in the
876Perl 5 object system. As with attributes, there are many ways to
1d68af04 877approach this topic, so we try to keep it pretty basic, while still
94b19069 878making it possible to extend the system in many ways.
879
552e3d24 880See L<Class::MOP::Method> for more details.
94b19069 881
828ecf13 882=head2 The Instance protocol
127d39a7 883
828ecf13 884This provides a layer of abstraction for creating object instances.
885Since the other layers use this protocol, it is relatively easy to
886change the type of your instances from the default hash reference to
887some other type of reference. Several examples are provided in the
888F<examples/> directory included in this distribution.
127d39a7 889
890See L<Class::MOP::Instance> for more details.
891
be7677c7 892=head1 FUNCTIONS
893
828ecf13 894Note that this module does not export any constants or functions.
895
c1d5345a 896=head2 Constants
897
898=over 4
899
828ecf13 900=item I<Class::MOP::IS_RUNNING_ON_5_10>
c1d5345a 901
828ecf13 902We set this constant depending on what version perl we are on, this
903allows us to take advantage of new 5.10 features and stay backwards
5b60bf98 904compatible.
c1d5345a 905
906=back
907
448b6e55 908=head2 Utility functions
909
bd07fbdb 910Note that these are all called as B<functions, not methods>.
081a927b 911
448b6e55 912=over 4
913
828ecf13 914=item B<Class::MOP::load_class($class_name)>
448b6e55 915
828ecf13 916This will load the specified C<$class_name>. This function can be used
917in place of tricks like C<eval "use $module"> or using C<require>
01f3a099 918unconditionally. This will return the metaclass of C<$class_name>.
448b6e55 919
828ecf13 920=item B<Class::MOP::is_class_loaded($class_name)>
448b6e55 921
15ab5451 922Returns a boolean indicating whether or not C<$class_name> has been
923loaded.
448b6e55 924
828ecf13 925This does a basic check of the symbol table to try and determine as
926best it can if the C<$class_name> is loaded, it is probably correct
927about 99% of the time, but it can be fooled into reporting false
928positives.
448b6e55 929
cdac22cc 930=item B<Class::MOP::get_code_info($code)>
931
932This function returns two values, the name of the package the C<$code>
933is from and the name of the C<$code> itself. This is used by several
5b60bf98 934elements of the MOP to determine where a given C<$code> reference is
cdac22cc 935from.
936
44ba77a3 937=item B<Class::MOP::class_of($instance_or_class_name)>
938
939This will return the metaclass of the given instance or class name.
940Even if the class lacks a metaclass, no metaclass will be initialized
941and C<undef> will be returned.
942
828ecf13 943=item B<Class::MOP::check_package_cache_flag($pkg)>
e0e4674a 944
bd07fbdb 945B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
946
828ecf13 947This will return an integer that is managed by L<Class::MOP::Class> to
948determine if a module's symbol table has been altered.
127d39a7 949
828ecf13 950In Perl 5.10 or greater, this flag is package specific. However in
951versions prior to 5.10, this will use the C<PL_sub_generation>
952variable which is not package specific.
127d39a7 953
828ecf13 954=item B<Class::MOP::load_first_existing_class(@class_names)>
063ad0c5 955
956B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
957
958Given a list of class names, this function will attempt to load each
959one in turn.
960
828ecf13 961If it finds a class it can load, it will return that class' name. If
962none of the classes can be loaded, it will throw an exception.
063ad0c5 963
448b6e55 964=back
965
966=head2 Metaclass cache functions
967
6c842677 968Class::MOP holds a cache of metaclasses. The following are functions
1d68af04 969(B<not methods>) which can be used to access that cache. It is not
6c842677 970recommended that you mess with these. Bad things could happen, but if
971you are brave and willing to risk it: go for it!
be7677c7 972
973=over 4
974
828ecf13 975=item B<Class::MOP::get_all_metaclasses>
be7677c7 976
6c842677 977This will return a hash of all the metaclass instances that have
828ecf13 978been cached by L<Class::MOP::Class>, keyed by the package name.
b9d9fc0b 979
828ecf13 980=item B<Class::MOP::get_all_metaclass_instances>
be7677c7 981
6c842677 982This will return a list of all the metaclass instances that have
828ecf13 983been cached by L<Class::MOP::Class>.
b9d9fc0b 984
828ecf13 985=item B<Class::MOP::get_all_metaclass_names>
be7677c7 986
6c842677 987This will return a list of all the metaclass names that have
828ecf13 988been cached by L<Class::MOP::Class>.
b9d9fc0b 989
828ecf13 990=item B<Class::MOP::get_metaclass_by_name($name)>
be7677c7 991
828ecf13 992This will return a cached L<Class::MOP::Class> instance, or nothing
6c842677 993if no metaclass exists with that C<$name>.
127d39a7 994
828ecf13 995=item B<Class::MOP::store_metaclass_by_name($name, $meta)>
be7677c7 996
127d39a7 997This will store a metaclass in the cache at the supplied C<$key>.
998
828ecf13 999=item B<Class::MOP::weaken_metaclass($name)>
be7677c7 1000
6c842677 1001In rare cases (e.g. anonymous metaclasses) it is desirable to
1002store a weakened reference in the metaclass cache. This
1003function will weaken the reference to the metaclass stored
1004in C<$name>.
127d39a7 1005
828ecf13 1006=item B<Class::MOP::does_metaclass_exist($name)>
be7677c7 1007
828ecf13 1008This will return true of there exists a metaclass stored in the
6c842677 1009C<$name> key, and return false otherwise.
127d39a7 1010
828ecf13 1011=item B<Class::MOP::remove_metaclass_by_name($name)>
be7677c7 1012
6c842677 1013This will remove the metaclass stored in the C<$name> key.
127d39a7 1014
be7677c7 1015=back
1016
552e3d24 1017=head1 SEE ALSO
8b978dd5 1018
552e3d24 1019=head2 Books
8b978dd5 1020
1d68af04 1021There are very few books out on Meta Object Protocols and Metaclasses
1022because it is such an esoteric topic. The following books are really
1023the only ones I have found. If you know of any more, B<I<please>>
a2e85e6c 1024email me and let me know, I would love to hear about them.
1025
8b978dd5 1026=over 4
1027
15ab5451 1028=item I<The Art of the Meta Object Protocol>
8b978dd5 1029
15ab5451 1030=item I<Advances in Object-Oriented Metalevel Architecture and Reflection>
8b978dd5 1031
15ab5451 1032=item I<Putting MetaClasses to Work>
b51af7f9 1033
15ab5451 1034=item I<Smalltalk: The Language>
a2e85e6c 1035
94b19069 1036=back
1037
550d56db 1038=head2 Papers
1039
1040=over 4
1041
15ab5451 1042=item "Uniform and safe metaclass composition"
550d56db 1043
1d68af04 1044An excellent paper by the people who brought us the original Traits paper.
1045This paper is on how Traits can be used to do safe metaclass composition,
1046and offers an excellent introduction section which delves into the topic of
550d56db 1047metaclass compatibility.
1048
1049L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
1050
15ab5451 1051=item "Safe Metaclass Programming"
550d56db 1052
1d68af04 1053This paper seems to precede the above paper, and propose a mix-in based
1054approach as opposed to the Traits based approach. Both papers have similar
1055information on the metaclass compatibility problem space.
550d56db 1056
1057L<http://citeseer.ist.psu.edu/37617.html>
1058
1059=back
1060
552e3d24 1061=head2 Prior Art
8b978dd5 1062
1063=over 4
1064
7184ca14 1065=item The Perl 6 MetaModel work in the Pugs project
8b978dd5 1066
1067=over 4
1068
552e3d24 1069=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
8b978dd5 1070
552e3d24 1071=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
8b978dd5 1072
1073=back
1074
94b19069 1075=back
1076
1d68af04 1077=head2 Articles
f8dfcfb7 1078
1079=over 4
1080
1d68af04 1081=item CPAN Module Review of Class::MOP
f8dfcfb7 1082
1083L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1084
1085=back
1086
a2e85e6c 1087=head1 SIMILAR MODULES
1088
1d68af04 1089As I have said above, this module is a class-builder-builder, so it is
1090not the same thing as modules like L<Class::Accessor> and
1091L<Class::MethodMaker>. That being said there are very few modules on CPAN
1092with similar goals to this module. The one I have found which is most
1093like this module is L<Class::Meta>, although it's philosophy and the MOP it
1094creates are very different from this modules.
94b19069 1095
a2e85e6c 1096=head1 BUGS
1097
1d68af04 1098All complex software has bugs lurking in it, and this module is no
53702399 1099exception.
1100
1101Please report any bugs to C<bug-class-mop@rt.cpan.org>, or through the
1102web interface at L<http://rt.cpan.org>.
1103
1104You can also discuss feature requests or possible bugs on the Moose
1105mailing list (moose@perl.org) or on IRC at
1106L<irc://irc.perl.org/#moose>.
a2e85e6c 1107
1108=head1 ACKNOWLEDGEMENTS
1109
1110=over 4
1111
b9d9fc0b 1112=item Rob Kinyon
a2e85e6c 1113
1d68af04 1114Thanks to Rob for actually getting the development of this module kick-started.
a2e85e6c 1115
1116=back
1117
1a09d9cc 1118=head1 AUTHORS
94b19069 1119
a2e85e6c 1120Stevan Little E<lt>stevan@iinteractive.comE<gt>
552e3d24 1121
9c8cda90 1122B<with contributions from:>
1123
1124Brandon (blblack) Black
1125
4f116037 1126Florian (rafl) Ragwitz
1127
9c8cda90 1128Guillermo (groditi) Roditi
1129
53702399 1130Dave (autarch) Rolsky
1131
9195ddff 1132Matt (mst) Trout
1133
9c8cda90 1134Rob (robkinyon) Kinyon
1135
1136Yuval (nothingmuch) Kogman
1a09d9cc 1137
f430cfa4 1138Scott (konobi) McWhirter
1139
94b19069 1140=head1 COPYRIGHT AND LICENSE
1141
070bb6c9 1142Copyright 2006-2009 by Infinity Interactive, Inc.
94b19069 1143
1144L<http://www.iinteractive.com>
1145
1146This library is free software; you can redistribute it and/or modify
1d68af04 1147it under the same terms as Perl itself.
94b19069 1148
1149=cut