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