12 use Devel::GlobalDestruction qw( in_global_destruction );
13 use Scalar::Util 'weaken', 'reftype', 'blessed';
14 use Sub::Name qw( subname );
16 use Class::MOP::Class;
17 use Class::MOP::Attribute;
18 use Class::MOP::Method;
20 use Class::MOP::Immutable;
23 *IS_RUNNING_ON_5_10 = ($] < 5.009_005)
27 *HAVE_ISAREV = defined(&mro::get_isarev)
31 # this is either part of core or set up appropriately by MRO::Compat
32 *check_package_cache_flag = \&mro::get_pkg_gen;
35 our $VERSION = '0.79';
36 our $XS_VERSION = $VERSION;
37 $VERSION = eval $VERSION;
38 our $AUTHORITY = 'cpan:STEVAN';
41 XSLoader::load( __PACKAGE__, $XS_VERSION );
45 # Metaclasses are singletons, so we cache them here.
46 # there is no need to worry about destruction though
47 # because they should die only when the program dies.
48 # After all, do package definitions even get reaped?
51 # means of accessing all the metaclasses that have
52 # been initialized thus far (for mugwumps obj browser)
53 sub get_all_metaclasses { %METAS }
54 sub get_all_metaclass_instances { values %METAS }
55 sub get_all_metaclass_names { keys %METAS }
56 sub get_metaclass_by_name { $METAS{$_[0]} }
57 sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
58 sub weaken_metaclass { weaken($METAS{$_[0]}) }
59 sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} }
60 sub remove_metaclass_by_name { $METAS{$_[0]} = undef }
63 # We only cache metaclasses, meaning instances of
64 # Class::MOP::Class. We do not cache instance of
65 # Class::MOP::Package or Class::MOP::Module. Mostly
66 # because I don't yet see a good reason to do so.
69 sub load_first_existing_class {
73 foreach my $class (@classes) {
74 unless ( _is_valid_class_name($class) ) {
75 my $display = defined($class) ? $class : 'undef';
76 confess "Invalid class name ($display)";
82 for my $class (@classes) {
83 my $e = _try_load_one_class($class);
86 $exceptions{$class} = $e;
94 return $found if $found;
100 "Could not load class (%s) because : %s", $_,
107 sub _try_load_one_class {
110 return if is_class_loaded($class);
112 my $file = $class . '.pm';
117 eval { require($file) };
123 my $class = load_first_existing_class($_[0]);
124 return get_metaclass_by_name($class) || $class;
127 sub _is_valid_class_name {
130 return 0 if ref($class);
131 return 0 unless defined($class);
132 return 0 unless length($class);
134 return 1 if $class =~ /^\w+(?:::\w+)*$/;
143 return undef if !defined($class);
145 $class = blessed($class) || $class;
147 return get_metaclass_by_name($class);
150 ## ----------------------------------------------------------------------------
151 ## Setting up our environment ...
152 ## ----------------------------------------------------------------------------
153 ## Class::MOP needs to have a few things in the global perl environment so
154 ## that it can operate effectively. Those things are done here.
155 ## ----------------------------------------------------------------------------
157 # ... nothing yet actually ;)
159 ## ----------------------------------------------------------------------------
161 ## ----------------------------------------------------------------------------
162 ## The code below here is to bootstrap our MOP with itself. This is also
163 ## sometimes called "tying the knot". By doing this, we make it much easier
164 ## to extend the MOP through subclassing and such since now you can use the
165 ## MOP itself to extend itself.
167 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
168 ## ----------------------------------------------------------------------------
170 # We need to add in the meta-attributes here so that
171 # any subclass of Class::MOP::* will be able to
172 # inherit them using &construct_instance
174 ## --------------------------------------------------------
175 ## Class::MOP::Package
177 Class::MOP::Package->meta->add_attribute(
178 Class::MOP::Attribute->new('package' => (
180 # NOTE: we need to do this in order
181 # for the instance meta-object to
182 # not fall into meta-circular death
184 # we just alias the original method
185 # rather than re-produce it here
186 'name' => \&Class::MOP::Package::name
191 Class::MOP::Package->meta->add_attribute(
192 Class::MOP::Attribute->new('namespace' => (
195 # we just alias the original method
196 # rather than re-produce it here
197 'namespace' => \&Class::MOP::Package::namespace
200 default => sub { \undef }
204 ## --------------------------------------------------------
205 ## Class::MOP::Module
208 # yeah this is kind of stretching things a bit,
209 # but truthfully the version should be an attribute
210 # of the Module, the weirdness comes from having to
211 # stick to Perl 5 convention and store it in the
212 # $VERSION package variable. Basically if you just
213 # squint at it, it will look how you want it to look.
214 # Either as a package variable, or as a attribute of
215 # the metaclass, isn't abstraction great :)
217 Class::MOP::Module->meta->add_attribute(
218 Class::MOP::Attribute->new('version' => (
221 # we just alias the original method
222 # rather than re-produce it here
223 'version' => \&Class::MOP::Module::version
226 default => sub { \undef }
231 # By following the same conventions as version here,
232 # we are opening up the possibility that people can
233 # use the $AUTHORITY in non-Class::MOP modules as
236 Class::MOP::Module->meta->add_attribute(
237 Class::MOP::Attribute->new('authority' => (
240 # we just alias the original method
241 # rather than re-produce it here
242 'authority' => \&Class::MOP::Module::authority
245 default => sub { \undef }
249 ## --------------------------------------------------------
252 Class::MOP::Class->meta->add_attribute(
253 Class::MOP::Attribute->new('attributes' => (
255 # NOTE: we need to do this in order
256 # for the instance meta-object to
257 # not fall into meta-circular death
259 # we just alias the original method
260 # rather than re-produce it here
261 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
263 default => sub { {} }
267 Class::MOP::Class->meta->add_attribute(
268 Class::MOP::Attribute->new('methods' => (
271 # we just alias the original method
272 # rather than re-produce it here
273 'get_method_map' => \&Class::MOP::Class::get_method_map
275 default => sub { {} }
279 Class::MOP::Class->meta->add_attribute(
280 Class::MOP::Attribute->new('superclasses' => (
283 # we just alias the original method
284 # rather than re-produce it here
285 'superclasses' => \&Class::MOP::Class::superclasses
288 default => sub { \undef }
292 Class::MOP::Class->meta->add_attribute(
293 Class::MOP::Attribute->new('attribute_metaclass' => (
296 # we just alias the original method
297 # rather than re-produce it here
298 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
300 default => 'Class::MOP::Attribute',
304 Class::MOP::Class->meta->add_attribute(
305 Class::MOP::Attribute->new('method_metaclass' => (
308 # we just alias the original method
309 # rather than re-produce it here
310 'method_metaclass' => \&Class::MOP::Class::method_metaclass
312 default => 'Class::MOP::Method',
316 Class::MOP::Class->meta->add_attribute(
317 Class::MOP::Attribute->new('wrapped_method_metaclass' => (
320 # we just alias the original method
321 # rather than re-produce it here
322 'wrapped_method_metaclass' => \&Class::MOP::Class::wrapped_method_metaclass
324 default => 'Class::MOP::Method::Wrapped',
328 Class::MOP::Class->meta->add_attribute(
329 Class::MOP::Attribute->new('instance_metaclass' => (
331 # NOTE: we need to do this in order
332 # for the instance meta-object to
333 # not fall into meta-circular death
335 # we just alias the original method
336 # rather than re-produce it here
337 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
339 default => 'Class::MOP::Instance',
343 Class::MOP::Class->meta->add_attribute(
344 Class::MOP::Attribute->new('immutable_transformer' => (
346 'immutable_transformer' => \&Class::MOP::Class::immutable_transformer
349 '_set_immutable_transformer' => \&Class::MOP::Class::_set_immutable_transformer
355 # we don't actually need to tie the knot with
356 # Class::MOP::Class here, it is actually handled
357 # within Class::MOP::Class itself in the
358 # construct_class_instance method.
360 ## --------------------------------------------------------
361 ## Class::MOP::Attribute
363 Class::MOP::Attribute->meta->add_attribute(
364 Class::MOP::Attribute->new('name' => (
366 # NOTE: we need to do this in order
367 # for the instance meta-object to
368 # not fall into meta-circular death
370 # we just alias the original method
371 # rather than re-produce it here
372 'name' => \&Class::MOP::Attribute::name
377 Class::MOP::Attribute->meta->add_attribute(
378 Class::MOP::Attribute->new('associated_class' => (
380 # NOTE: we need to do this in order
381 # for the instance meta-object to
382 # not fall into meta-circular death
384 # we just alias the original method
385 # rather than re-produce it here
386 'associated_class' => \&Class::MOP::Attribute::associated_class
391 Class::MOP::Attribute->meta->add_attribute(
392 Class::MOP::Attribute->new('accessor' => (
393 reader => { 'accessor' => \&Class::MOP::Attribute::accessor },
394 predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
398 Class::MOP::Attribute->meta->add_attribute(
399 Class::MOP::Attribute->new('reader' => (
400 reader => { 'reader' => \&Class::MOP::Attribute::reader },
401 predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
405 Class::MOP::Attribute->meta->add_attribute(
406 Class::MOP::Attribute->new('initializer' => (
407 reader => { 'initializer' => \&Class::MOP::Attribute::initializer },
408 predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
412 Class::MOP::Attribute->meta->add_attribute(
413 Class::MOP::Attribute->new('definition_context' => (
414 reader => { 'definition_context' => \&Class::MOP::Attribute::definition_context },
418 Class::MOP::Attribute->meta->add_attribute(
419 Class::MOP::Attribute->new('writer' => (
420 reader => { 'writer' => \&Class::MOP::Attribute::writer },
421 predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
425 Class::MOP::Attribute->meta->add_attribute(
426 Class::MOP::Attribute->new('predicate' => (
427 reader => { 'predicate' => \&Class::MOP::Attribute::predicate },
428 predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate },
432 Class::MOP::Attribute->meta->add_attribute(
433 Class::MOP::Attribute->new('clearer' => (
434 reader => { 'clearer' => \&Class::MOP::Attribute::clearer },
435 predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer },
439 Class::MOP::Attribute->meta->add_attribute(
440 Class::MOP::Attribute->new('builder' => (
441 reader => { 'builder' => \&Class::MOP::Attribute::builder },
442 predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder },
446 Class::MOP::Attribute->meta->add_attribute(
447 Class::MOP::Attribute->new('init_arg' => (
448 reader => { 'init_arg' => \&Class::MOP::Attribute::init_arg },
449 predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg },
453 Class::MOP::Attribute->meta->add_attribute(
454 Class::MOP::Attribute->new('default' => (
455 # default has a custom 'reader' method ...
456 predicate => { 'has_default' => \&Class::MOP::Attribute::has_default },
460 Class::MOP::Attribute->meta->add_attribute(
461 Class::MOP::Attribute->new('associated_methods' => (
462 reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods },
463 default => sub { [] }
467 Class::MOP::Attribute->meta->add_method('clone' => sub {
469 $self->meta->clone_object($self, @_);
472 ## --------------------------------------------------------
473 ## Class::MOP::Method
474 Class::MOP::Method->meta->add_attribute(
475 Class::MOP::Attribute->new('body' => (
476 reader => { 'body' => \&Class::MOP::Method::body },
480 Class::MOP::Method->meta->add_attribute(
481 Class::MOP::Attribute->new('associated_metaclass' => (
482 reader => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass },
486 Class::MOP::Method->meta->add_attribute(
487 Class::MOP::Attribute->new('package_name' => (
488 reader => { 'package_name' => \&Class::MOP::Method::package_name },
492 Class::MOP::Method->meta->add_attribute(
493 Class::MOP::Attribute->new('name' => (
494 reader => { 'name' => \&Class::MOP::Method::name },
498 Class::MOP::Method->meta->add_attribute(
499 Class::MOP::Attribute->new('original_method' => (
500 reader => { 'original_method' => \&Class::MOP::Method::original_method },
501 writer => { '_set_original_method' => \&Class::MOP::Method::_set_original_method },
505 Class::MOP::Method->meta->add_method('clone' => sub {
507 my $clone = $self->meta->clone_object($self, @_);
508 $clone->_set_original_method($self);
512 ## --------------------------------------------------------
513 ## Class::MOP::Method::Wrapped
516 # the way this item is initialized, this
517 # really does not follow the standard
518 # practices of attributes, but we put
519 # it here for completeness
520 Class::MOP::Method::Wrapped->meta->add_attribute(
521 Class::MOP::Attribute->new('modifier_table')
524 ## --------------------------------------------------------
525 ## Class::MOP::Method::Generated
527 Class::MOP::Method::Generated->meta->add_attribute(
528 Class::MOP::Attribute->new('is_inline' => (
529 reader => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline },
534 Class::MOP::Method::Generated->meta->add_attribute(
535 Class::MOP::Attribute->new('definition_context' => (
536 reader => { 'definition_context' => \&Class::MOP::Method::Generated::definition_context },
540 ## --------------------------------------------------------
541 ## Class::MOP::Method::Accessor
543 Class::MOP::Method::Accessor->meta->add_attribute(
544 Class::MOP::Attribute->new('attribute' => (
546 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
551 Class::MOP::Method::Accessor->meta->add_attribute(
552 Class::MOP::Attribute->new('accessor_type' => (
553 reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
557 ## --------------------------------------------------------
558 ## Class::MOP::Method::Constructor
560 Class::MOP::Method::Constructor->meta->add_attribute(
561 Class::MOP::Attribute->new('options' => (
563 'options' => \&Class::MOP::Method::Constructor::options
565 default => sub { +{} }
569 Class::MOP::Method::Constructor->meta->add_attribute(
570 Class::MOP::Attribute->new('associated_metaclass' => (
571 init_arg => "metaclass", # FIXME alias and rename
573 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
578 ## --------------------------------------------------------
579 ## Class::MOP::Instance
582 # these don't yet do much of anything, but are just
583 # included for completeness
585 Class::MOP::Instance->meta->add_attribute(
586 Class::MOP::Attribute->new('associated_metaclass',
587 reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
591 Class::MOP::Instance->meta->add_attribute(
592 Class::MOP::Attribute->new('_class_name',
594 reader => { _class_name => \&Class::MOP::Instance::_class_name },
595 #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway
596 #default => sub { $_[0]->associated_metaclass->name },
600 Class::MOP::Instance->meta->add_attribute(
601 Class::MOP::Attribute->new('attributes',
602 reader => { attributes => \&Class::MOP::Instance::get_all_attributes },
606 Class::MOP::Instance->meta->add_attribute(
607 Class::MOP::Attribute->new('slots',
608 reader => { slots => \&Class::MOP::Instance::slots },
612 Class::MOP::Instance->meta->add_attribute(
613 Class::MOP::Attribute->new('slot_hash',
614 reader => { slot_hash => \&Class::MOP::Instance::slot_hash },
619 # we need the meta instance of the meta instance to be created now, in order
620 # for the constructor to be able to use it
621 Class::MOP::Instance->meta->get_meta_instance;
623 # pretend the add_method never happenned. it hasn't yet affected anything
624 undef Class::MOP::Instance->meta->{_package_cache_flag};
626 ## --------------------------------------------------------
627 ## Now close all the Class::MOP::* classes
629 # NOTE: we don't need to inline the the accessors this only lengthens
630 # the compile time of the MOP, and gives us no actual benefits.
632 $_->meta->make_immutable(
633 inline_constructor => 1,
634 replace_constructor => 1,
635 constructor_name => "_new",
636 inline_accessors => 0,
642 Class::MOP::Attribute
648 Class::MOP::Method::Generated
650 Class::MOP::Method::Accessor
651 Class::MOP::Method::Constructor
652 Class::MOP::Method::Wrapped
663 Class::MOP - A Meta Object Protocol for Perl 5
667 This module is a fully functioning meta object protocol for the
668 Perl 5 object system. It makes no attempt to change the behavior or
669 characteristics of the Perl 5 object system, only to create a
670 protocol for its manipulation and introspection.
672 That said, it does attempt to create the tools for building a rich set
673 of extensions to the Perl 5 object system. Every attempt has been made
674 to abide by the spirit of the Perl 5 object system that we all know
677 This documentation is sparse on conceptual details. We suggest looking
678 at the items listed in the L<SEE ALSO> section for more
679 information. In particular the book "The Art of the Meta Object
680 Protocol" was very influential in the development of this system.
682 =head2 What is a Meta Object Protocol?
684 A meta object protocol is an API to an object system.
686 To be more specific, it abstracts the components of an object system
687 (classes, object, methods, object attributes, etc.). These
688 abstractions can then be used to inspect and manipulate the object
689 system which they describe.
691 It can be said that there are two MOPs for any object system; the
692 implicit MOP and the explicit MOP. The implicit MOP handles things
693 like method dispatch or inheritance, which happen automatically as
694 part of how the object system works. The explicit MOP typically
695 handles the introspection/reflection features of the object system.
697 All object systems have implicit MOPs. Without one, they would not
698 work. Explicit MOPs are much less common, and depending on the
699 language can vary from restrictive (Reflection in Java or C#) to wide
700 open (CLOS is a perfect example).
702 =head2 Yet Another Class Builder! Why?
704 This is B<not> a class builder so much as a I<class builder
705 B<builder>>. The intent is that an end user will not use this module
706 directly, but instead this module is used by module authors to build
707 extensions and features onto the Perl 5 object system.
709 This system is used by L<Moose>, which supplies a powerful class
710 builder system built entirely on top of C<Class::MOP>.
712 =head2 Who is this module for?
714 This module is for anyone who has ever created or wanted to create a
715 module for the Class:: namespace. The tools which this module provides
716 make doing complex Perl 5 wizardry simpler, by removing such barriers
717 as the need to hack symbol tables, or understand the fine details of
720 =head2 What changes do I have to make to use this module?
722 This module was designed to be as unintrusive as possible. Many of its
723 features are accessible without B<any> change to your existing
724 code. It is meant to be a compliment to your existing code and not an
725 intrusion on your code base. Unlike many other B<Class::> modules,
726 this module B<does not> require you subclass it, or even that you
727 C<use> it in within your module's package.
729 The only features which requires additions to your code are the
730 attribute handling and instance construction features, and these are
731 both completely optional features. The only reason for this is because
732 Perl 5's object system does not actually have these features built
733 in. More information about this feature can be found below.
735 =head2 About Performance
737 It is a common misconception that explicit MOPs are a performance hit.
738 This is not a universal truth, it is a side-effect of some specific
739 implementations. For instance, using Java reflection is slow because
740 the JVM cannot take advantage of any compiler optimizations, and the
741 JVM has to deal with much more runtime type information as well.
743 Reflection in C# is marginally better as it was designed into the
744 language and runtime (the CLR). In contrast, CLOS (the Common Lisp
745 Object System) was built to support an explicit MOP, and so
746 performance is tuned for it.
748 This library in particular does its absolute best to avoid putting
749 B<any> drain at all upon your code's performance. In fact, by itself
750 it does nothing to affect your existing code. So you only pay for what
753 =head2 About Metaclass compatibility
755 This module makes sure that all metaclasses created are both upwards
756 and downwards compatible. The topic of metaclass compatibility is
757 highly esoteric and is something only encountered when doing deep and
758 involved metaclass hacking. There are two basic kinds of metaclass
759 incompatibility; upwards and downwards.
761 Upwards metaclass compatibility means that the metaclass of a
762 given class is either the same as (or a subclass of) all of the
765 Downward metaclass compatibility means that the metaclasses of a
766 given class's ancestors are all either the same as (or a subclass
769 Here is a diagram showing a set of two classes (C<A> and C<B>) and
770 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
771 metaclass compatibility both upwards and downwards.
773 +---------+ +---------+
774 | Meta::A |<----| Meta::B | <....... (instance of )
775 +---------+ +---------+ <------- (inherits from)
778 +---------+ +---------+
780 +---------+ +---------+
782 As I said this is a highly esoteric topic and one you will only run
783 into if you do a lot of subclassing of L<Class::MOP::Class>. If you
784 are interested in why this is an issue see the paper I<Uniform and
785 safe metaclass composition> linked to in the L<SEE ALSO> section of
788 =head2 Using custom metaclasses
790 Always use the L<metaclass> pragma when using a custom metaclass, this
791 will ensure the proper initialization order and not accidentally
792 create an incorrect type of metaclass for you. This is a very rare
793 problem, and one which can only occur if you are doing deep metaclass
794 programming. So in other words, don't worry about it.
796 Note that if you're using L<Moose> we encourage you to I<not> use
797 L<metaclass> pragma, and instead use L<Moose::Util::MetaRole> to apply
798 roles to a class's metaclasses. This topic is covered at length in
799 various L<Moose::Cookbook> recipes.
803 The meta-object protocol is divided into 4 main sub-protocols:
805 =head2 The Class protocol
807 This provides a means of manipulating and introspecting a Perl 5
808 class. It handles symbol table hacking for you, and provides a rich
809 set of methods that go beyond simple package introspection.
811 See L<Class::MOP::Class> for more details.
813 =head2 The Attribute protocol
815 This provides a consistent representation for an attribute of a Perl 5
816 class. Since there are so many ways to create and handle attributes in
817 Perl 5 OO, the Attribute protocol provide as much of a unified
818 approach as possible. Of course, you are always free to extend this
819 protocol by subclassing the appropriate classes.
821 See L<Class::MOP::Attribute> for more details.
823 =head2 The Method protocol
825 This provides a means of manipulating and introspecting methods in the
826 Perl 5 object system. As with attributes, there are many ways to
827 approach this topic, so we try to keep it pretty basic, while still
828 making it possible to extend the system in many ways.
830 See L<Class::MOP::Method> for more details.
832 =head2 The Instance protocol
834 This provides a layer of abstraction for creating object instances.
835 Since the other layers use this protocol, it is relatively easy to
836 change the type of your instances from the default hash reference to
837 some other type of reference. Several examples are provided in the
838 F<examples/> directory included in this distribution.
840 See L<Class::MOP::Instance> for more details.
844 Note that this module does not export any constants or functions.
850 =item I<Class::MOP::IS_RUNNING_ON_5_10>
852 We set this constant depending on what version perl we are on, this
853 allows us to take advantage of new 5.10 features and stay backwards
856 =item I<Class::MOP::HAVE_ISAREV>
858 Whether or not the L<mro> pragma provides C<get_isarev>, a much faster
859 way to get all the subclasses of a certain class.
863 =head2 Utility functions
865 Note that these are all called as B<functions, not methods>.
869 =item B<Class::MOP::load_class($class_name)>
871 This will load the specified C<$class_name>. This function can be used
872 in place of tricks like C<eval "use $module"> or using C<require>
875 =item B<Class::MOP::is_class_loaded($class_name)>
877 Returns a boolean indicating whether or not C<$class_name> has been
880 This does a basic check of the symbol table to try and determine as
881 best it can if the C<$class_name> is loaded, it is probably correct
882 about 99% of the time, but it can be fooled into reporting false
885 =item B<Class::MOP::get_code_info($code)>
887 This function returns two values, the name of the package the C<$code>
888 is from and the name of the C<$code> itself. This is used by several
889 elements of the MOP to determine where a given C<$code> reference is
892 =item B<Class::MOP::class_of($instance_or_class_name)>
894 This will return the metaclass of the given instance or class name.
895 Even if the class lacks a metaclass, no metaclass will be initialized
896 and C<undef> will be returned.
898 =item B<Class::MOP::check_package_cache_flag($pkg)>
900 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
902 This will return an integer that is managed by L<Class::MOP::Class> to
903 determine if a module's symbol table has been altered.
905 In Perl 5.10 or greater, this flag is package specific. However in
906 versions prior to 5.10, this will use the C<PL_sub_generation>
907 variable which is not package specific.
909 =item B<Class::MOP::load_first_existing_class(@class_names)>
911 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
913 Given a list of class names, this function will attempt to load each
916 If it finds a class it can load, it will return that class' name. If
917 none of the classes can be loaded, it will throw an exception.
921 =head2 Metaclass cache functions
923 Class::MOP holds a cache of metaclasses. The following are functions
924 (B<not methods>) which can be used to access that cache. It is not
925 recommended that you mess with these. Bad things could happen, but if
926 you are brave and willing to risk it: go for it!
930 =item B<Class::MOP::get_all_metaclasses>
932 This will return a hash of all the metaclass instances that have
933 been cached by L<Class::MOP::Class>, keyed by the package name.
935 =item B<Class::MOP::get_all_metaclass_instances>
937 This will return a list of all the metaclass instances that have
938 been cached by L<Class::MOP::Class>.
940 =item B<Class::MOP::get_all_metaclass_names>
942 This will return a list of all the metaclass names that have
943 been cached by L<Class::MOP::Class>.
945 =item B<Class::MOP::get_metaclass_by_name($name)>
947 This will return a cached L<Class::MOP::Class> instance, or nothing
948 if no metaclass exists with that C<$name>.
950 =item B<Class::MOP::store_metaclass_by_name($name, $meta)>
952 This will store a metaclass in the cache at the supplied C<$key>.
954 =item B<Class::MOP::weaken_metaclass($name)>
956 In rare cases (e.g. anonymous metaclasses) it is desirable to
957 store a weakened reference in the metaclass cache. This
958 function will weaken the reference to the metaclass stored
961 =item B<Class::MOP::does_metaclass_exist($name)>
963 This will return true of there exists a metaclass stored in the
964 C<$name> key, and return false otherwise.
966 =item B<Class::MOP::remove_metaclass_by_name($name)>
968 This will remove the metaclass stored in the C<$name> key.
976 There are very few books out on Meta Object Protocols and Metaclasses
977 because it is such an esoteric topic. The following books are really
978 the only ones I have found. If you know of any more, B<I<please>>
979 email me and let me know, I would love to hear about them.
983 =item I<The Art of the Meta Object Protocol>
985 =item I<Advances in Object-Oriented Metalevel Architecture and Reflection>
987 =item I<Putting MetaClasses to Work>
989 =item I<Smalltalk: The Language>
997 =item "Uniform and safe metaclass composition"
999 An excellent paper by the people who brought us the original Traits paper.
1000 This paper is on how Traits can be used to do safe metaclass composition,
1001 and offers an excellent introduction section which delves into the topic of
1002 metaclass compatibility.
1004 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
1006 =item "Safe Metaclass Programming"
1008 This paper seems to precede the above paper, and propose a mix-in based
1009 approach as opposed to the Traits based approach. Both papers have similar
1010 information on the metaclass compatibility problem space.
1012 L<http://citeseer.ist.psu.edu/37617.html>
1020 =item The Perl 6 MetaModel work in the Pugs project
1024 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
1026 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
1036 =item CPAN Module Review of Class::MOP
1038 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1042 =head1 SIMILAR MODULES
1044 As I have said above, this module is a class-builder-builder, so it is
1045 not the same thing as modules like L<Class::Accessor> and
1046 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1047 with similar goals to this module. The one I have found which is most
1048 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1049 creates are very different from this modules.
1053 All complex software has bugs lurking in it, and this module is no
1054 exception. If you find a bug please either email me, or add the bug
1057 =head1 ACKNOWLEDGEMENTS
1063 Thanks to Rob for actually getting the development of this module kick-started.
1069 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1071 B<with contributions from:>
1073 Brandon (blblack) Black
1075 Florian (rafl) Ragwitz
1077 Guillermo (groditi) Roditi
1081 Rob (robkinyon) Kinyon
1083 Yuval (nothingmuch) Kogman
1085 Scott (konobi) McWhirter
1087 =head1 COPYRIGHT AND LICENSE
1089 Copyright 2006-2009 by Infinity Interactive, Inc.
1091 L<http://www.iinteractive.com>
1093 This library is free software; you can redistribute it and/or modify
1094 it under the same terms as Perl itself.