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