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