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