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