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