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