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