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