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