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