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