Merge the topic/mi-methods-attributes branch.
[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
9b871d79 15use Class::MOP::Mixin::AttributeCore;
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## --------------------------------------------------------
9b871d79 166## Class::MOP::Mixin::HasMethods
727919c5 167
9b871d79 168Class::MOP::Mixin::HasMethods->meta->add_attribute(
169 Class::MOP::Attribute->new('_methods' => (
b880e0de 170 reader => {
9b871d79 171 # NOTE:
ce2ae40f 172 # we just alias the original method
1d68af04 173 # rather than re-produce it here
9b871d79 174 '_full_method_map' => \&Class::MOP::Mixin::HasMethods::_full_method_map
b880e0de 175 },
9b871d79 176 default => sub { {} }
727919c5 177 ))
178);
179
9b871d79 180Class::MOP::Mixin::HasMethods->meta->add_attribute(
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
9b871d79 186 'method_metaclass' => \&Class::MOP::Mixin::HasMethods::method_metaclass
a5e51f0b 187 },
9b871d79 188 default => 'Class::MOP::Method',
a5e51f0b 189 ))
190);
191
9b871d79 192Class::MOP::Mixin::HasMethods->meta->add_attribute(
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
9b871d79 198 'wrapped_method_metaclass' => \&Class::MOP::Mixin::HasMethods::wrapped_method_metaclass
bcef1f7c 199 },
9b871d79 200 default => 'Class::MOP::Method::Wrapped',
bcef1f7c 201 ))
202);
203
9b871d79 204## --------------------------------------------------------
205## Class::MOP::Mixin::HasMethods
206
207Class::MOP::Mixin::HasAttributes->meta->add_attribute(
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
216 '_attribute_map' => \&Class::MOP::Mixin::HasAttributes::_attribute_map
217 },
218 default => sub { {} }
219 ))
220);
221
222Class::MOP::Mixin::HasAttributes->meta->add_attribute(
223 Class::MOP::Attribute->new('attribute_metaclass' => (
b1ff395f 224 reader => {
225 # NOTE:
226 # we just alias the original method
227 # rather than re-produce it here
9b871d79 228 'attribute_metaclass' => \&Class::MOP::Mixin::HasAttributes::attribute_metaclass
b1ff395f 229 },
9b871d79 230 default => 'Class::MOP::Attribute',
b1ff395f 231 ))
232);
233
9b871d79 234## --------------------------------------------------------
235## Class::MOP::Package
236
b1ff395f 237Class::MOP::Package->meta->add_attribute(
9b871d79 238 Class::MOP::Attribute->new('package' => (
b1ff395f 239 reader => {
9b871d79 240 # NOTE: we need to do this in order
241 # for the instance meta-object to
242 # not fall into meta-circular death
243 #
244 # we just alias the original method
245 # rather than re-produce it here
246 'name' => \&Class::MOP::Package::name
247 },
248 ))
249);
250
251Class::MOP::Package->meta->add_attribute(
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
9b871d79 257 'namespace' => \&Class::MOP::Package::namespace
b1ff395f 258 },
9b871d79 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## --------------------------------------------------------
9b871d79 383## Class::MOP::Mixin::AttributeCore
384Class::MOP::Mixin::AttributeCore->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
9b871d79 393 'name' => \&Class::MOP::Mixin::AttributeCore::name
b880e0de 394 }
7b31baf4 395 ))
396);
397
9b871d79 398Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 399 Class::MOP::Attribute->new('accessor' => (
9b871d79 400 reader => { 'accessor' => \&Class::MOP::Mixin::AttributeCore::accessor },
401 predicate => { 'has_accessor' => \&Class::MOP::Mixin::AttributeCore::has_accessor },
7b31baf4 402 ))
403);
404
9b871d79 405Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 406 Class::MOP::Attribute->new('reader' => (
9b871d79 407 reader => { 'reader' => \&Class::MOP::Mixin::AttributeCore::reader },
408 predicate => { 'has_reader' => \&Class::MOP::Mixin::AttributeCore::has_reader },
7b31baf4 409 ))
410);
411
9b871d79 412Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 413 Class::MOP::Attribute->new('initializer' => (
9b871d79 414 reader => { 'initializer' => \&Class::MOP::Mixin::AttributeCore::initializer },
415 predicate => { 'has_initializer' => \&Class::MOP::Mixin::AttributeCore::has_initializer },
0ab65f99 416 ))
417);
418
9b871d79 419Class::MOP::Mixin::AttributeCore->meta->add_attribute(
d9d99689 420 Class::MOP::Attribute->new('definition_context' => (
9b871d79 421 reader => { 'definition_context' => \&Class::MOP::Mixin::AttributeCore::definition_context },
d9d99689 422 ))
423);
424
9b871d79 425Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 426 Class::MOP::Attribute->new('writer' => (
9b871d79 427 reader => { 'writer' => \&Class::MOP::Mixin::AttributeCore::writer },
428 predicate => { 'has_writer' => \&Class::MOP::Mixin::AttributeCore::has_writer },
7b31baf4 429 ))
430);
431
9b871d79 432Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 433 Class::MOP::Attribute->new('predicate' => (
9b871d79 434 reader => { 'predicate' => \&Class::MOP::Mixin::AttributeCore::predicate },
435 predicate => { 'has_predicate' => \&Class::MOP::Mixin::AttributeCore::has_predicate },
7b31baf4 436 ))
437);
438
9b871d79 439Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 440 Class::MOP::Attribute->new('clearer' => (
9b871d79 441 reader => { 'clearer' => \&Class::MOP::Mixin::AttributeCore::clearer },
442 predicate => { 'has_clearer' => \&Class::MOP::Mixin::AttributeCore::has_clearer },
7d28758b 443 ))
444);
445
9b871d79 446Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 447 Class::MOP::Attribute->new('builder' => (
9b871d79 448 reader => { 'builder' => \&Class::MOP::Mixin::AttributeCore::builder },
449 predicate => { 'has_builder' => \&Class::MOP::Mixin::AttributeCore::has_builder },
1d68af04 450 ))
451);
452
9b871d79 453Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 454 Class::MOP::Attribute->new('init_arg' => (
9b871d79 455 reader => { 'init_arg' => \&Class::MOP::Mixin::AttributeCore::init_arg },
456 predicate => { 'has_init_arg' => \&Class::MOP::Mixin::AttributeCore::has_init_arg },
7b31baf4 457 ))
458);
459
9b871d79 460Class::MOP::Mixin::AttributeCore->meta->add_attribute(
8683db0e 461 Class::MOP::Attribute->new('default' => (
7b31baf4 462 # default has a custom 'reader' method ...
9b871d79 463 predicate => { 'has_default' => \&Class::MOP::Mixin::AttributeCore::has_default },
7b31baf4 464 ))
465);
466
9b871d79 467Class::MOP::Mixin::AttributeCore->meta->add_attribute(
468 Class::MOP::Attribute->new('insertion_order' => (
469 reader => { 'insertion_order' => \&Class::MOP::Mixin::AttributeCore::insertion_order },
470 writer => { '_set_insertion_order' => \&Class::MOP::Mixin::AttributeCore::_set_insertion_order },
471 predicate => { 'has_insertion_order' => \&Class::MOP::Mixin::AttributeCore::has_insertion_order },
472 ))
473);
474
475## --------------------------------------------------------
476## Class::MOP::Attribute
3545c727 477Class::MOP::Attribute->meta->add_attribute(
9b871d79 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(
9b871d79 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
9b871d79 697$_->meta->make_immutable(
698 inline_constructor => 0,
699 constructor_name => undef,
700 inline_accessors => 0,
701) for qw/
702 Class::MOP::Mixin
703 Class::MOP::Mixin::AttributeCore
704 Class::MOP::Mixin::HasAttributes
705 Class::MOP::Mixin::HasMethods
706/;
707
94b19069 7081;
709
710__END__
711
712=pod
713
1d68af04 714=head1 NAME
94b19069 715
716Class::MOP - A Meta Object Protocol for Perl 5
717
5b60bf98 718=head1 DESCRIPTION
94b19069 719
127d39a7 720This module is a fully functioning meta object protocol for the
1d68af04 721Perl 5 object system. It makes no attempt to change the behavior or
722characteristics of the Perl 5 object system, only to create a
27e31eaf 723protocol for its manipulation and introspection.
94b19069 724
828ecf13 725That said, it does attempt to create the tools for building a rich set
726of extensions to the Perl 5 object system. Every attempt has been made
727to abide by the spirit of the Perl 5 object system that we all know
728and love.
94b19069 729
828ecf13 730This documentation is sparse on conceptual details. We suggest looking
731at the items listed in the L<SEE ALSO> section for more
732information. In particular the book "The Art of the Meta Object
733Protocol" was very influential in the development of this system.
40483095 734
bfe4d0fc 735=head2 What is a Meta Object Protocol?
736
1d68af04 737A meta object protocol is an API to an object system.
bfe4d0fc 738
828ecf13 739To be more specific, it abstracts the components of an object system
740(classes, object, methods, object attributes, etc.). These
741abstractions can then be used to inspect and manipulate the object
742system which they describe.
bfe4d0fc 743
1d68af04 744It can be said that there are two MOPs for any object system; the
828ecf13 745implicit MOP and the explicit MOP. The implicit MOP handles things
1d68af04 746like method dispatch or inheritance, which happen automatically as
747part of how the object system works. The explicit MOP typically
748handles the introspection/reflection features of the object system.
bfe4d0fc 749
828ecf13 750All object systems have implicit MOPs. Without one, they would not
5b60bf98 751work. Explicit MOPs are much less common, and depending on the
752language can vary from restrictive (Reflection in Java or C#) to wide
753open (CLOS is a perfect example).
e16da3e6 754
828ecf13 755=head2 Yet Another Class Builder! Why?
756
757This is B<not> a class builder so much as a I<class builder
758B<builder>>. The intent is that an end user will not use this module
759directly, but instead this module is used by module authors to build
760extensions and features onto the Perl 5 object system.
761
762This system is used by L<Moose>, which supplies a powerful class
763builder system built entirely on top of C<Class::MOP>.
e16da3e6 764
94b19069 765=head2 Who is this module for?
766
828ecf13 767This module is for anyone who has ever created or wanted to create a
768module for the Class:: namespace. The tools which this module provides
769make doing complex Perl 5 wizardry simpler, by removing such barriers
770as the need to hack symbol tables, or understand the fine details of
771method dispatch.
94b19069 772
bfe4d0fc 773=head2 What changes do I have to make to use this module?
774
828ecf13 775This module was designed to be as unintrusive as possible. Many of its
5b60bf98 776features are accessible without B<any> change to your existing
828ecf13 777code. It is meant to be a compliment to your existing code and not an
778intrusion on your code base. Unlike many other B<Class::> modules,
779this module B<does not> require you subclass it, or even that you
780C<use> it in within your module's package.
bfe4d0fc 781
1d68af04 782The only features which requires additions to your code are the
2eb717d5 783attribute handling and instance construction features, and these are
1d68af04 784both completely optional features. The only reason for this is because
785Perl 5's object system does not actually have these features built
2eb717d5 786in. More information about this feature can be found below.
bfe4d0fc 787
828ecf13 788=head2 About Performance
789
5b60bf98 790It is a common misconception that explicit MOPs are a performance hit.
828ecf13 791This is not a universal truth, it is a side-effect of some specific
792implementations. For instance, using Java reflection is slow because
793the JVM cannot take advantage of any compiler optimizations, and the
794JVM has to deal with much more runtime type information as well.
bfe4d0fc 795
828ecf13 796Reflection in C# is marginally better as it was designed into the
797language and runtime (the CLR). In contrast, CLOS (the Common Lisp
798Object System) was built to support an explicit MOP, and so
799performance is tuned for it.
1d68af04 800
828ecf13 801This library in particular does its absolute best to avoid putting
1d68af04 802B<any> drain at all upon your code's performance. In fact, by itself
828ecf13 803it does nothing to affect your existing code. So you only pay for what
804you actually use.
bfe4d0fc 805
550d56db 806=head2 About Metaclass compatibility
807
1d68af04 808This module makes sure that all metaclasses created are both upwards
809and downwards compatible. The topic of metaclass compatibility is
810highly esoteric and is something only encountered when doing deep and
811involved metaclass hacking. There are two basic kinds of metaclass
812incompatibility; upwards and downwards.
550d56db 813
1d68af04 814Upwards metaclass compatibility means that the metaclass of a
815given class is either the same as (or a subclass of) all of the
550d56db 816class's ancestors.
817
1d68af04 818Downward metaclass compatibility means that the metaclasses of a
828ecf13 819given class's ancestors are all either the same as (or a subclass
550d56db 820of) that metaclass.
821
1d68af04 822Here is a diagram showing a set of two classes (C<A> and C<B>) and
823two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
550d56db 824metaclass compatibility both upwards and downwards.
825
826 +---------+ +---------+
827 | Meta::A |<----| Meta::B | <....... (instance of )
1d68af04 828 +---------+ +---------+ <------- (inherits from)
550d56db 829 ^ ^
830 : :
831 +---------+ +---------+
832 | A |<----| B |
833 +---------+ +---------+
834
1d68af04 835As I said this is a highly esoteric topic and one you will only run
828ecf13 836into if you do a lot of subclassing of L<Class::MOP::Class>. If you
837are interested in why this is an issue see the paper I<Uniform and
838safe metaclass composition> linked to in the L<SEE ALSO> section of
839this document.
550d56db 840
aa448b16 841=head2 Using custom metaclasses
842
828ecf13 843Always use the L<metaclass> pragma when using a custom metaclass, this
5b60bf98 844will ensure the proper initialization order and not accidentally
845create an incorrect type of metaclass for you. This is a very rare
846problem, and one which can only occur if you are doing deep metaclass
aa448b16 847programming. So in other words, don't worry about it.
848
828ecf13 849Note that if you're using L<Moose> we encourage you to I<not> use
850L<metaclass> pragma, and instead use L<Moose::Util::MetaRole> to apply
851roles to a class's metaclasses. This topic is covered at length in
852various L<Moose::Cookbook> recipes.
853
94b19069 854=head1 PROTOCOLS
855
828ecf13 856The meta-object protocol is divided into 4 main sub-protocols:
94b19069 857
828ecf13 858=head2 The Class protocol
94b19069 859
1d68af04 860This provides a means of manipulating and introspecting a Perl 5
828ecf13 861class. It handles symbol table hacking for you, and provides a rich
862set of methods that go beyond simple package introspection.
94b19069 863
552e3d24 864See L<Class::MOP::Class> for more details.
865
828ecf13 866=head2 The Attribute protocol
94b19069 867
828ecf13 868This provides a consistent representation for an attribute of a Perl 5
869class. Since there are so many ways to create and handle attributes in
870Perl 5 OO, the Attribute protocol provide as much of a unified
871approach as possible. Of course, you are always free to extend this
872protocol by subclassing the appropriate classes.
94b19069 873
552e3d24 874See L<Class::MOP::Attribute> for more details.
875
828ecf13 876=head2 The Method protocol
94b19069 877
828ecf13 878This provides a means of manipulating and introspecting methods in the
879Perl 5 object system. As with attributes, there are many ways to
1d68af04 880approach this topic, so we try to keep it pretty basic, while still
94b19069 881making it possible to extend the system in many ways.
882
552e3d24 883See L<Class::MOP::Method> for more details.
94b19069 884
828ecf13 885=head2 The Instance protocol
127d39a7 886
828ecf13 887This provides a layer of abstraction for creating object instances.
888Since the other layers use this protocol, it is relatively easy to
889change the type of your instances from the default hash reference to
890some other type of reference. Several examples are provided in the
891F<examples/> directory included in this distribution.
127d39a7 892
893See L<Class::MOP::Instance> for more details.
894
be7677c7 895=head1 FUNCTIONS
896
828ecf13 897Note that this module does not export any constants or functions.
898
c1d5345a 899=head2 Constants
900
901=over 4
902
828ecf13 903=item I<Class::MOP::IS_RUNNING_ON_5_10>
c1d5345a 904
828ecf13 905We set this constant depending on what version perl we are on, this
906allows us to take advantage of new 5.10 features and stay backwards
5b60bf98 907compatible.
c1d5345a 908
909=back
910
448b6e55 911=head2 Utility functions
912
bd07fbdb 913Note that these are all called as B<functions, not methods>.
081a927b 914
448b6e55 915=over 4
916
828ecf13 917=item B<Class::MOP::load_class($class_name)>
448b6e55 918
9d441c23 919This will load the specified C<$class_name>, if it is not already
920loaded (as reported by C<is_class_loaded>). This function can be used
828ecf13 921in place of tricks like C<eval "use $module"> or using C<require>
7716a8f9 922unconditionally.
448b6e55 923
674d9359 924If the module cannot be loaded, an exception is thrown.
925
78527c84 926For historical reasons, this function explicitly returns a true value.
674d9359 927
828ecf13 928=item B<Class::MOP::is_class_loaded($class_name)>
448b6e55 929
15ab5451 930Returns a boolean indicating whether or not C<$class_name> has been
931loaded.
448b6e55 932
828ecf13 933This does a basic check of the symbol table to try and determine as
934best it can if the C<$class_name> is loaded, it is probably correct
935about 99% of the time, but it can be fooled into reporting false
9d441c23 936positives. In particular, loading any of the core L<IO> modules will
937cause most of the rest of the core L<IO> modules to falsely report
938having been loaded, due to the way the base L<IO> module works.
448b6e55 939
cdac22cc 940=item B<Class::MOP::get_code_info($code)>
941
942This function returns two values, the name of the package the C<$code>
943is from and the name of the C<$code> itself. This is used by several
5b60bf98 944elements of the MOP to determine where a given C<$code> reference is
cdac22cc 945from.
946
44ba77a3 947=item B<Class::MOP::class_of($instance_or_class_name)>
948
3fa5b3f9 949This will return the metaclass of the given instance or class name. If the
950class lacks a metaclass, no metaclass will be initialized, and C<undef> will be
951returned.
44ba77a3 952
828ecf13 953=item B<Class::MOP::check_package_cache_flag($pkg)>
e0e4674a 954
bd07fbdb 955B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
956
828ecf13 957This will return an integer that is managed by L<Class::MOP::Class> to
958determine if a module's symbol table has been altered.
127d39a7 959
828ecf13 960In Perl 5.10 or greater, this flag is package specific. However in
961versions prior to 5.10, this will use the C<PL_sub_generation>
962variable which is not package specific.
127d39a7 963
828ecf13 964=item B<Class::MOP::load_first_existing_class(@class_names)>
063ad0c5 965
966B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
967
968Given a list of class names, this function will attempt to load each
969one in turn.
970
828ecf13 971If it finds a class it can load, it will return that class' name. If
972none of the classes can be loaded, it will throw an exception.
063ad0c5 973
448b6e55 974=back
975
976=head2 Metaclass cache functions
977
6c842677 978Class::MOP holds a cache of metaclasses. The following are functions
1d68af04 979(B<not methods>) which can be used to access that cache. It is not
6c842677 980recommended that you mess with these. Bad things could happen, but if
981you are brave and willing to risk it: go for it!
be7677c7 982
983=over 4
984
828ecf13 985=item B<Class::MOP::get_all_metaclasses>
be7677c7 986
6c842677 987This will return a hash of all the metaclass instances that have
828ecf13 988been cached by L<Class::MOP::Class>, keyed by the package name.
b9d9fc0b 989
828ecf13 990=item B<Class::MOP::get_all_metaclass_instances>
be7677c7 991
6c842677 992This will return a list of all the metaclass instances that have
828ecf13 993been cached by L<Class::MOP::Class>.
b9d9fc0b 994
828ecf13 995=item B<Class::MOP::get_all_metaclass_names>
be7677c7 996
6c842677 997This will return a list of all the metaclass names that have
828ecf13 998been cached by L<Class::MOP::Class>.
b9d9fc0b 999
828ecf13 1000=item B<Class::MOP::get_metaclass_by_name($name)>
be7677c7 1001
828ecf13 1002This will return a cached L<Class::MOP::Class> instance, or nothing
6c842677 1003if no metaclass exists with that C<$name>.
127d39a7 1004
828ecf13 1005=item B<Class::MOP::store_metaclass_by_name($name, $meta)>
be7677c7 1006
127d39a7 1007This will store a metaclass in the cache at the supplied C<$key>.
1008
828ecf13 1009=item B<Class::MOP::weaken_metaclass($name)>
be7677c7 1010
6c842677 1011In rare cases (e.g. anonymous metaclasses) it is desirable to
1012store a weakened reference in the metaclass cache. This
1013function will weaken the reference to the metaclass stored
1014in C<$name>.
127d39a7 1015
828ecf13 1016=item B<Class::MOP::does_metaclass_exist($name)>
be7677c7 1017
828ecf13 1018This will return true of there exists a metaclass stored in the
6c842677 1019C<$name> key, and return false otherwise.
127d39a7 1020
828ecf13 1021=item B<Class::MOP::remove_metaclass_by_name($name)>
be7677c7 1022
6c842677 1023This will remove the metaclass stored in the C<$name> key.
127d39a7 1024
be7677c7 1025=back
1026
552e3d24 1027=head1 SEE ALSO
8b978dd5 1028
552e3d24 1029=head2 Books
8b978dd5 1030
1d68af04 1031There are very few books out on Meta Object Protocols and Metaclasses
1032because it is such an esoteric topic. The following books are really
1033the only ones I have found. If you know of any more, B<I<please>>
a2e85e6c 1034email me and let me know, I would love to hear about them.
1035
8b978dd5 1036=over 4
1037
15ab5451 1038=item I<The Art of the Meta Object Protocol>
8b978dd5 1039
15ab5451 1040=item I<Advances in Object-Oriented Metalevel Architecture and Reflection>
8b978dd5 1041
15ab5451 1042=item I<Putting MetaClasses to Work>
b51af7f9 1043
15ab5451 1044=item I<Smalltalk: The Language>
a2e85e6c 1045
94b19069 1046=back
1047
550d56db 1048=head2 Papers
1049
1050=over 4
1051
15ab5451 1052=item "Uniform and safe metaclass composition"
550d56db 1053
1d68af04 1054An excellent paper by the people who brought us the original Traits paper.
1055This paper is on how Traits can be used to do safe metaclass composition,
1056and offers an excellent introduction section which delves into the topic of
550d56db 1057metaclass compatibility.
1058
1059L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
1060
15ab5451 1061=item "Safe Metaclass Programming"
550d56db 1062
1d68af04 1063This paper seems to precede the above paper, and propose a mix-in based
1064approach as opposed to the Traits based approach. Both papers have similar
1065information on the metaclass compatibility problem space.
550d56db 1066
1067L<http://citeseer.ist.psu.edu/37617.html>
1068
1069=back
1070
552e3d24 1071=head2 Prior Art
8b978dd5 1072
1073=over 4
1074
7184ca14 1075=item The Perl 6 MetaModel work in the Pugs project
8b978dd5 1076
1077=over 4
1078
02615ef0 1079=item L<http://svn.openfoundry.org/pugs/misc/Perl-MetaModel/>
8b978dd5 1080
552e3d24 1081=item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
8b978dd5 1082
1083=back
1084
94b19069 1085=back
1086
1d68af04 1087=head2 Articles
f8dfcfb7 1088
1089=over 4
1090
1d68af04 1091=item CPAN Module Review of Class::MOP
f8dfcfb7 1092
1093L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1094
1095=back
1096
a2e85e6c 1097=head1 SIMILAR MODULES
1098
1d68af04 1099As I have said above, this module is a class-builder-builder, so it is
1100not the same thing as modules like L<Class::Accessor> and
1101L<Class::MethodMaker>. That being said there are very few modules on CPAN
1102with similar goals to this module. The one I have found which is most
1103like this module is L<Class::Meta>, although it's philosophy and the MOP it
1104creates are very different from this modules.
94b19069 1105
a2e85e6c 1106=head1 BUGS
1107
1d68af04 1108All complex software has bugs lurking in it, and this module is no
53702399 1109exception.
1110
1111Please report any bugs to C<bug-class-mop@rt.cpan.org>, or through the
1112web interface at L<http://rt.cpan.org>.
1113
1114You can also discuss feature requests or possible bugs on the Moose
1115mailing list (moose@perl.org) or on IRC at
1116L<irc://irc.perl.org/#moose>.
a2e85e6c 1117
1118=head1 ACKNOWLEDGEMENTS
1119
1120=over 4
1121
b9d9fc0b 1122=item Rob Kinyon
a2e85e6c 1123
1d68af04 1124Thanks to Rob for actually getting the development of this module kick-started.
a2e85e6c 1125
1126=back
1127
1a09d9cc 1128=head1 AUTHORS
94b19069 1129
a2e85e6c 1130Stevan Little E<lt>stevan@iinteractive.comE<gt>
552e3d24 1131
9c8cda90 1132B<with contributions from:>
1133
1134Brandon (blblack) Black
1135
4f116037 1136Florian (rafl) Ragwitz
1137
9c8cda90 1138Guillermo (groditi) Roditi
1139
53702399 1140Dave (autarch) Rolsky
1141
9195ddff 1142Matt (mst) Trout
1143
9c8cda90 1144Rob (robkinyon) Kinyon
1145
1146Yuval (nothingmuch) Kogman
1a09d9cc 1147
f430cfa4 1148Scott (konobi) McWhirter
1149
20f5ccef 1150Dylan Hardison
1151
94b19069 1152=head1 COPYRIGHT AND LICENSE
1153
070bb6c9 1154Copyright 2006-2009 by Infinity Interactive, Inc.
94b19069 1155
1156L<http://www.iinteractive.com>
1157
1158This library is free software; you can redistribute it and/or modify
1d68af04 1159it under the same terms as Perl itself.
94b19069 1160
1161=cut