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