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