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 }
62 # This handles instances as well as class names
64 my $class = blessed($_[0]) || $_[0];
65 return $METAS{$class};
69 # We only cache metaclasses, meaning instances of
70 # Class::MOP::Class. We do not cache instance of
71 # Class::MOP::Package or Class::MOP::Module. Mostly
72 # because I don't yet see a good reason to do so.
75 sub load_first_existing_class {
79 foreach my $class (@classes) {
80 unless ( _is_valid_class_name($class) ) {
81 my $display = defined($class) ? $class : 'undef';
82 confess "Invalid class name ($display)";
88 for my $class (@classes) {
89 my $e = _try_load_one_class($class);
92 $exceptions{$class} = $e;
100 return $found if $found;
106 "Could not load class (%s) because : %s", $_,
113 sub _try_load_one_class {
116 return if is_class_loaded($class);
118 my $file = $class . '.pm';
123 eval { require($file) };
129 my $class = load_first_existing_class($_[0]);
130 return get_metaclass_by_name($class) || $class;
133 sub _is_valid_class_name {
136 return 0 if ref($class);
137 return 0 unless defined($class);
138 return 0 unless length($class);
140 return 1 if $class =~ /^\w+(?:::\w+)*$/;
145 ## ----------------------------------------------------------------------------
146 ## Setting up our environment ...
147 ## ----------------------------------------------------------------------------
148 ## Class::MOP needs to have a few things in the global perl environment so
149 ## that it can operate effectively. Those things are done here.
150 ## ----------------------------------------------------------------------------
152 # ... nothing yet actually ;)
154 ## ----------------------------------------------------------------------------
156 ## ----------------------------------------------------------------------------
157 ## The code below here is to bootstrap our MOP with itself. This is also
158 ## sometimes called "tying the knot". By doing this, we make it much easier
159 ## to extend the MOP through subclassing and such since now you can use the
160 ## MOP itself to extend itself.
162 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
163 ## ----------------------------------------------------------------------------
165 # We need to add in the meta-attributes here so that
166 # any subclass of Class::MOP::* will be able to
167 # inherit them using &construct_instance
169 ## --------------------------------------------------------
170 ## Class::MOP::Package
172 Class::MOP::Package->meta->add_attribute(
173 Class::MOP::Attribute->new('package' => (
175 # NOTE: we need to do this in order
176 # for the instance meta-object to
177 # not fall into meta-circular death
179 # we just alias the original method
180 # rather than re-produce it here
181 'name' => \&Class::MOP::Package::name
186 Class::MOP::Package->meta->add_attribute(
187 Class::MOP::Attribute->new('namespace' => (
190 # we just alias the original method
191 # rather than re-produce it here
192 'namespace' => \&Class::MOP::Package::namespace
195 default => sub { \undef }
199 ## --------------------------------------------------------
200 ## Class::MOP::Module
203 # yeah this is kind of stretching things a bit,
204 # but truthfully the version should be an attribute
205 # of the Module, the weirdness comes from having to
206 # stick to Perl 5 convention and store it in the
207 # $VERSION package variable. Basically if you just
208 # squint at it, it will look how you want it to look.
209 # Either as a package variable, or as a attribute of
210 # the metaclass, isn't abstraction great :)
212 Class::MOP::Module->meta->add_attribute(
213 Class::MOP::Attribute->new('version' => (
216 # we just alias the original method
217 # rather than re-produce it here
218 'version' => \&Class::MOP::Module::version
221 default => sub { \undef }
226 # By following the same conventions as version here,
227 # we are opening up the possibility that people can
228 # use the $AUTHORITY in non-Class::MOP modules as
231 Class::MOP::Module->meta->add_attribute(
232 Class::MOP::Attribute->new('authority' => (
235 # we just alias the original method
236 # rather than re-produce it here
237 'authority' => \&Class::MOP::Module::authority
240 default => sub { \undef }
244 ## --------------------------------------------------------
247 Class::MOP::Class->meta->add_attribute(
248 Class::MOP::Attribute->new('attributes' => (
250 # NOTE: we need to do this in order
251 # for the instance meta-object to
252 # not fall into meta-circular death
254 # we just alias the original method
255 # rather than re-produce it here
256 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
258 default => sub { {} }
262 Class::MOP::Class->meta->add_attribute(
263 Class::MOP::Attribute->new('methods' => (
266 # we just alias the original method
267 # rather than re-produce it here
268 'get_method_map' => \&Class::MOP::Class::get_method_map
270 default => sub { {} }
274 Class::MOP::Class->meta->add_attribute(
275 Class::MOP::Attribute->new('superclasses' => (
278 # we just alias the original method
279 # rather than re-produce it here
280 'superclasses' => \&Class::MOP::Class::superclasses
283 default => sub { \undef }
287 Class::MOP::Class->meta->add_attribute(
288 Class::MOP::Attribute->new('attribute_metaclass' => (
291 # we just alias the original method
292 # rather than re-produce it here
293 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
295 default => 'Class::MOP::Attribute',
299 Class::MOP::Class->meta->add_attribute(
300 Class::MOP::Attribute->new('method_metaclass' => (
303 # we just alias the original method
304 # rather than re-produce it here
305 'method_metaclass' => \&Class::MOP::Class::method_metaclass
307 default => 'Class::MOP::Method',
311 Class::MOP::Class->meta->add_attribute(
312 Class::MOP::Attribute->new('wrapped_method_metaclass' => (
315 # we just alias the original method
316 # rather than re-produce it here
317 'wrapped_method_metaclass' => \&Class::MOP::Class::wrapped_method_metaclass
319 default => 'Class::MOP::Method::Wrapped',
323 Class::MOP::Class->meta->add_attribute(
324 Class::MOP::Attribute->new('instance_metaclass' => (
326 # NOTE: we need to do this in order
327 # for the instance meta-object to
328 # not fall into meta-circular death
330 # we just alias the original method
331 # rather than re-produce it here
332 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
334 default => 'Class::MOP::Instance',
338 Class::MOP::Class->meta->add_attribute(
339 Class::MOP::Attribute->new('immutable_transformer' => (
341 'immutable_transformer' => \&Class::MOP::Class::immutable_transformer
344 '_set_immutable_transformer' => \&Class::MOP::Class::_set_immutable_transformer
350 # we don't actually need to tie the knot with
351 # Class::MOP::Class here, it is actually handled
352 # within Class::MOP::Class itself in the
353 # construct_class_instance method.
355 ## --------------------------------------------------------
356 ## Class::MOP::Attribute
358 Class::MOP::Attribute->meta->add_attribute(
359 Class::MOP::Attribute->new('name' => (
361 # NOTE: we need to do this in order
362 # for the instance meta-object to
363 # not fall into meta-circular death
365 # we just alias the original method
366 # rather than re-produce it here
367 'name' => \&Class::MOP::Attribute::name
372 Class::MOP::Attribute->meta->add_attribute(
373 Class::MOP::Attribute->new('associated_class' => (
375 # NOTE: we need to do this in order
376 # for the instance meta-object to
377 # not fall into meta-circular death
379 # we just alias the original method
380 # rather than re-produce it here
381 'associated_class' => \&Class::MOP::Attribute::associated_class
386 Class::MOP::Attribute->meta->add_attribute(
387 Class::MOP::Attribute->new('accessor' => (
388 reader => { 'accessor' => \&Class::MOP::Attribute::accessor },
389 predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
393 Class::MOP::Attribute->meta->add_attribute(
394 Class::MOP::Attribute->new('reader' => (
395 reader => { 'reader' => \&Class::MOP::Attribute::reader },
396 predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
400 Class::MOP::Attribute->meta->add_attribute(
401 Class::MOP::Attribute->new('initializer' => (
402 reader => { 'initializer' => \&Class::MOP::Attribute::initializer },
403 predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
407 Class::MOP::Attribute->meta->add_attribute(
408 Class::MOP::Attribute->new('definition_context' => (
409 reader => { 'definition_context' => \&Class::MOP::Attribute::definition_context },
413 Class::MOP::Attribute->meta->add_attribute(
414 Class::MOP::Attribute->new('writer' => (
415 reader => { 'writer' => \&Class::MOP::Attribute::writer },
416 predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
420 Class::MOP::Attribute->meta->add_attribute(
421 Class::MOP::Attribute->new('predicate' => (
422 reader => { 'predicate' => \&Class::MOP::Attribute::predicate },
423 predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate },
427 Class::MOP::Attribute->meta->add_attribute(
428 Class::MOP::Attribute->new('clearer' => (
429 reader => { 'clearer' => \&Class::MOP::Attribute::clearer },
430 predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer },
434 Class::MOP::Attribute->meta->add_attribute(
435 Class::MOP::Attribute->new('builder' => (
436 reader => { 'builder' => \&Class::MOP::Attribute::builder },
437 predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder },
441 Class::MOP::Attribute->meta->add_attribute(
442 Class::MOP::Attribute->new('init_arg' => (
443 reader => { 'init_arg' => \&Class::MOP::Attribute::init_arg },
444 predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg },
448 Class::MOP::Attribute->meta->add_attribute(
449 Class::MOP::Attribute->new('default' => (
450 # default has a custom 'reader' method ...
451 predicate => { 'has_default' => \&Class::MOP::Attribute::has_default },
455 Class::MOP::Attribute->meta->add_attribute(
456 Class::MOP::Attribute->new('associated_methods' => (
457 reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods },
458 default => sub { [] }
462 Class::MOP::Attribute->meta->add_method('clone' => sub {
464 $self->meta->clone_object($self, @_);
467 ## --------------------------------------------------------
468 ## Class::MOP::Method
469 Class::MOP::Method->meta->add_attribute(
470 Class::MOP::Attribute->new('body' => (
471 reader => { 'body' => \&Class::MOP::Method::body },
475 Class::MOP::Method->meta->add_attribute(
476 Class::MOP::Attribute->new('associated_metaclass' => (
477 reader => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass },
481 Class::MOP::Method->meta->add_attribute(
482 Class::MOP::Attribute->new('package_name' => (
483 reader => { 'package_name' => \&Class::MOP::Method::package_name },
487 Class::MOP::Method->meta->add_attribute(
488 Class::MOP::Attribute->new('name' => (
489 reader => { 'name' => \&Class::MOP::Method::name },
493 Class::MOP::Method->meta->add_attribute(
494 Class::MOP::Attribute->new('original_method' => (
495 reader => { 'original_method' => \&Class::MOP::Method::original_method },
496 writer => { '_set_original_method' => \&Class::MOP::Method::_set_original_method },
500 Class::MOP::Method->meta->add_method('clone' => sub {
502 my $clone = $self->meta->clone_object($self, @_);
503 $clone->_set_original_method($self);
507 ## --------------------------------------------------------
508 ## Class::MOP::Method::Wrapped
511 # the way this item is initialized, this
512 # really does not follow the standard
513 # practices of attributes, but we put
514 # it here for completeness
515 Class::MOP::Method::Wrapped->meta->add_attribute(
516 Class::MOP::Attribute->new('modifier_table')
519 ## --------------------------------------------------------
520 ## Class::MOP::Method::Generated
522 Class::MOP::Method::Generated->meta->add_attribute(
523 Class::MOP::Attribute->new('is_inline' => (
524 reader => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline },
529 Class::MOP::Method::Generated->meta->add_attribute(
530 Class::MOP::Attribute->new('definition_context' => (
531 reader => { 'definition_context' => \&Class::MOP::Method::Generated::definition_context },
535 ## --------------------------------------------------------
536 ## Class::MOP::Method::Accessor
538 Class::MOP::Method::Accessor->meta->add_attribute(
539 Class::MOP::Attribute->new('attribute' => (
541 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
546 Class::MOP::Method::Accessor->meta->add_attribute(
547 Class::MOP::Attribute->new('accessor_type' => (
548 reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
552 ## --------------------------------------------------------
553 ## Class::MOP::Method::Constructor
555 Class::MOP::Method::Constructor->meta->add_attribute(
556 Class::MOP::Attribute->new('options' => (
558 'options' => \&Class::MOP::Method::Constructor::options
560 default => sub { +{} }
564 Class::MOP::Method::Constructor->meta->add_attribute(
565 Class::MOP::Attribute->new('associated_metaclass' => (
566 init_arg => "metaclass", # FIXME alias and rename
568 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
573 ## --------------------------------------------------------
574 ## Class::MOP::Instance
577 # these don't yet do much of anything, but are just
578 # included for completeness
580 Class::MOP::Instance->meta->add_attribute(
581 Class::MOP::Attribute->new('associated_metaclass',
582 reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
586 Class::MOP::Instance->meta->add_attribute(
587 Class::MOP::Attribute->new('_class_name',
589 reader => { _class_name => \&Class::MOP::Instance::_class_name },
590 #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway
591 #default => sub { $_[0]->associated_metaclass->name },
595 Class::MOP::Instance->meta->add_attribute(
596 Class::MOP::Attribute->new('attributes',
597 reader => { attributes => \&Class::MOP::Instance::get_all_attributes },
601 Class::MOP::Instance->meta->add_attribute(
602 Class::MOP::Attribute->new('slots',
603 reader => { slots => \&Class::MOP::Instance::slots },
607 Class::MOP::Instance->meta->add_attribute(
608 Class::MOP::Attribute->new('slot_hash',
609 reader => { slot_hash => \&Class::MOP::Instance::slot_hash },
614 # we need the meta instance of the meta instance to be created now, in order
615 # for the constructor to be able to use it
616 Class::MOP::Instance->meta->get_meta_instance;
618 # pretend the add_method never happenned. it hasn't yet affected anything
619 undef Class::MOP::Instance->meta->{_package_cache_flag};
621 ## --------------------------------------------------------
622 ## Now close all the Class::MOP::* classes
624 # NOTE: we don't need to inline the the accessors this only lengthens
625 # the compile time of the MOP, and gives us no actual benefits.
627 $_->meta->make_immutable(
628 inline_constructor => 1,
629 replace_constructor => 1,
630 constructor_name => "_new",
631 inline_accessors => 0,
637 Class::MOP::Attribute
643 Class::MOP::Method::Generated
645 Class::MOP::Method::Accessor
646 Class::MOP::Method::Constructor
647 Class::MOP::Method::Wrapped
658 Class::MOP - A Meta Object Protocol for Perl 5
662 This module is a fully functioning meta object protocol for the
663 Perl 5 object system. It makes no attempt to change the behavior or
664 characteristics of the Perl 5 object system, only to create a
665 protocol for its manipulation and introspection.
667 That said, it does attempt to create the tools for building a rich set
668 of extensions to the Perl 5 object system. Every attempt has been made
669 to abide by the spirit of the Perl 5 object system that we all know
672 This documentation is sparse on conceptual details. We suggest looking
673 at the items listed in the L<SEE ALSO> section for more
674 information. In particular the book "The Art of the Meta Object
675 Protocol" was very influential in the development of this system.
677 =head2 What is a Meta Object Protocol?
679 A meta object protocol is an API to an object system.
681 To be more specific, it abstracts the components of an object system
682 (classes, object, methods, object attributes, etc.). These
683 abstractions can then be used to inspect and manipulate the object
684 system which they describe.
686 It can be said that there are two MOPs for any object system; the
687 implicit MOP and the explicit MOP. The implicit MOP handles things
688 like method dispatch or inheritance, which happen automatically as
689 part of how the object system works. The explicit MOP typically
690 handles the introspection/reflection features of the object system.
692 All object systems have implicit MOPs. Without one, they would not
693 work. Explicit MOPs are much less common, and depending on the
694 language can vary from restrictive (Reflection in Java or C#) to wide
695 open (CLOS is a perfect example).
697 =head2 Yet Another Class Builder! Why?
699 This is B<not> a class builder so much as a I<class builder
700 B<builder>>. The intent is that an end user will not use this module
701 directly, but instead this module is used by module authors to build
702 extensions and features onto the Perl 5 object system.
704 This system is used by L<Moose>, which supplies a powerful class
705 builder system built entirely on top of C<Class::MOP>.
707 =head2 Who is this module for?
709 This module is for anyone who has ever created or wanted to create a
710 module for the Class:: namespace. The tools which this module provides
711 make doing complex Perl 5 wizardry simpler, by removing such barriers
712 as the need to hack symbol tables, or understand the fine details of
715 =head2 What changes do I have to make to use this module?
717 This module was designed to be as unintrusive as possible. Many of its
718 features are accessible without B<any> change to your existing
719 code. It is meant to be a compliment to your existing code and not an
720 intrusion on your code base. Unlike many other B<Class::> modules,
721 this module B<does not> require you subclass it, or even that you
722 C<use> it in within your module's package.
724 The only features which requires additions to your code are the
725 attribute handling and instance construction features, and these are
726 both completely optional features. The only reason for this is because
727 Perl 5's object system does not actually have these features built
728 in. More information about this feature can be found below.
730 =head2 About Performance
732 It is a common misconception that explicit MOPs are a performance hit.
733 This is not a universal truth, it is a side-effect of some specific
734 implementations. For instance, using Java reflection is slow because
735 the JVM cannot take advantage of any compiler optimizations, and the
736 JVM has to deal with much more runtime type information as well.
738 Reflection in C# is marginally better as it was designed into the
739 language and runtime (the CLR). In contrast, CLOS (the Common Lisp
740 Object System) was built to support an explicit MOP, and so
741 performance is tuned for it.
743 This library in particular does its absolute best to avoid putting
744 B<any> drain at all upon your code's performance. In fact, by itself
745 it does nothing to affect your existing code. So you only pay for what
748 =head2 About Metaclass compatibility
750 This module makes sure that all metaclasses created are both upwards
751 and downwards compatible. The topic of metaclass compatibility is
752 highly esoteric and is something only encountered when doing deep and
753 involved metaclass hacking. There are two basic kinds of metaclass
754 incompatibility; upwards and downwards.
756 Upwards metaclass compatibility means that the metaclass of a
757 given class is either the same as (or a subclass of) all of the
760 Downward metaclass compatibility means that the metaclasses of a
761 given class's ancestors are all either the same as (or a subclass
764 Here is a diagram showing a set of two classes (C<A> and C<B>) and
765 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
766 metaclass compatibility both upwards and downwards.
768 +---------+ +---------+
769 | Meta::A |<----| Meta::B | <....... (instance of )
770 +---------+ +---------+ <------- (inherits from)
773 +---------+ +---------+
775 +---------+ +---------+
777 As I said this is a highly esoteric topic and one you will only run
778 into if you do a lot of subclassing of L<Class::MOP::Class>. If you
779 are interested in why this is an issue see the paper I<Uniform and
780 safe metaclass composition> linked to in the L<SEE ALSO> section of
783 =head2 Using custom metaclasses
785 Always use the L<metaclass> pragma when using a custom metaclass, this
786 will ensure the proper initialization order and not accidentally
787 create an incorrect type of metaclass for you. This is a very rare
788 problem, and one which can only occur if you are doing deep metaclass
789 programming. So in other words, don't worry about it.
791 Note that if you're using L<Moose> we encourage you to I<not> use
792 L<metaclass> pragma, and instead use L<Moose::Util::MetaRole> to apply
793 roles to a class's metaclasses. This topic is covered at length in
794 various L<Moose::Cookbook> recipes.
798 The meta-object protocol is divided into 4 main sub-protocols:
800 =head2 The Class protocol
802 This provides a means of manipulating and introspecting a Perl 5
803 class. It handles symbol table hacking for you, and provides a rich
804 set of methods that go beyond simple package introspection.
806 See L<Class::MOP::Class> for more details.
808 =head2 The Attribute protocol
810 This provides a consistent representation for an attribute of a Perl 5
811 class. Since there are so many ways to create and handle attributes in
812 Perl 5 OO, the Attribute protocol provide as much of a unified
813 approach as possible. Of course, you are always free to extend this
814 protocol by subclassing the appropriate classes.
816 See L<Class::MOP::Attribute> for more details.
818 =head2 The Method protocol
820 This provides a means of manipulating and introspecting methods in the
821 Perl 5 object system. As with attributes, there are many ways to
822 approach this topic, so we try to keep it pretty basic, while still
823 making it possible to extend the system in many ways.
825 See L<Class::MOP::Method> for more details.
827 =head2 The Instance protocol
829 This provides a layer of abstraction for creating object instances.
830 Since the other layers use this protocol, it is relatively easy to
831 change the type of your instances from the default hash reference to
832 some other type of reference. Several examples are provided in the
833 F<examples/> directory included in this distribution.
835 See L<Class::MOP::Instance> for more details.
839 Note that this module does not export any constants or functions.
845 =item I<Class::MOP::IS_RUNNING_ON_5_10>
847 We set this constant depending on what version perl we are on, this
848 allows us to take advantage of new 5.10 features and stay backwards
851 =item I<Class::MOP::HAVE_ISAREV>
853 Whether or not the L<mro> pragma provides C<get_isarev>, a much faster
854 way to get all the subclasses of a certain class.
858 =head2 Utility functions
860 Note that these are all called as B<functions, not methods>.
864 =item B<Class::MOP::load_class($class_name)>
866 This will load the specified C<$class_name>. This function can be used
867 in place of tricks like C<eval "use $module"> or using C<require>
868 unconditionally. This will return the metaclass of C<$class_name>.
870 =item B<Class::MOP::is_class_loaded($class_name)>
872 Returns a boolean indicating whether or not C<$class_name> has been
875 This does a basic check of the symbol table to try and determine as
876 best it can if the C<$class_name> is loaded, it is probably correct
877 about 99% of the time, but it can be fooled into reporting false
880 =item B<Class::MOP::get_code_info($code)>
882 This function returns two values, the name of the package the C<$code>
883 is from and the name of the C<$code> itself. This is used by several
884 elements of the MOP to determine where a given C<$code> reference is
887 =item B<Class::MOP::class_of($instance_or_class_name)>
889 This will return the metaclass of the given instance or class name.
890 Even if the class lacks a metaclass, no metaclass will be initialized
891 and C<undef> will be returned.
893 =item B<Class::MOP::check_package_cache_flag($pkg)>
895 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
897 This will return an integer that is managed by L<Class::MOP::Class> to
898 determine if a module's symbol table has been altered.
900 In Perl 5.10 or greater, this flag is package specific. However in
901 versions prior to 5.10, this will use the C<PL_sub_generation>
902 variable which is not package specific.
904 =item B<Class::MOP::load_first_existing_class(@class_names)>
906 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
908 Given a list of class names, this function will attempt to load each
911 If it finds a class it can load, it will return that class' name. If
912 none of the classes can be loaded, it will throw an exception.
916 =head2 Metaclass cache functions
918 Class::MOP holds a cache of metaclasses. The following are functions
919 (B<not methods>) which can be used to access that cache. It is not
920 recommended that you mess with these. Bad things could happen, but if
921 you are brave and willing to risk it: go for it!
925 =item B<Class::MOP::get_all_metaclasses>
927 This will return a hash of all the metaclass instances that have
928 been cached by L<Class::MOP::Class>, keyed by the package name.
930 =item B<Class::MOP::get_all_metaclass_instances>
932 This will return a list of all the metaclass instances that have
933 been cached by L<Class::MOP::Class>.
935 =item B<Class::MOP::get_all_metaclass_names>
937 This will return a list of all the metaclass names that have
938 been cached by L<Class::MOP::Class>.
940 =item B<Class::MOP::get_metaclass_by_name($name)>
942 This will return a cached L<Class::MOP::Class> instance, or nothing
943 if no metaclass exists with that C<$name>.
945 =item B<Class::MOP::store_metaclass_by_name($name, $meta)>
947 This will store a metaclass in the cache at the supplied C<$key>.
949 =item B<Class::MOP::weaken_metaclass($name)>
951 In rare cases (e.g. anonymous metaclasses) it is desirable to
952 store a weakened reference in the metaclass cache. This
953 function will weaken the reference to the metaclass stored
956 =item B<Class::MOP::does_metaclass_exist($name)>
958 This will return true of there exists a metaclass stored in the
959 C<$name> key, and return false otherwise.
961 =item B<Class::MOP::remove_metaclass_by_name($name)>
963 This will remove the metaclass stored in the C<$name> key.
971 There are very few books out on Meta Object Protocols and Metaclasses
972 because it is such an esoteric topic. The following books are really
973 the only ones I have found. If you know of any more, B<I<please>>
974 email me and let me know, I would love to hear about them.
978 =item I<The Art of the Meta Object Protocol>
980 =item I<Advances in Object-Oriented Metalevel Architecture and Reflection>
982 =item I<Putting MetaClasses to Work>
984 =item I<Smalltalk: The Language>
992 =item "Uniform and safe metaclass composition"
994 An excellent paper by the people who brought us the original Traits paper.
995 This paper is on how Traits can be used to do safe metaclass composition,
996 and offers an excellent introduction section which delves into the topic of
997 metaclass compatibility.
999 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
1001 =item "Safe Metaclass Programming"
1003 This paper seems to precede the above paper, and propose a mix-in based
1004 approach as opposed to the Traits based approach. Both papers have similar
1005 information on the metaclass compatibility problem space.
1007 L<http://citeseer.ist.psu.edu/37617.html>
1015 =item The Perl 6 MetaModel work in the Pugs project
1019 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
1021 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
1031 =item CPAN Module Review of Class::MOP
1033 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1037 =head1 SIMILAR MODULES
1039 As I have said above, this module is a class-builder-builder, so it is
1040 not the same thing as modules like L<Class::Accessor> and
1041 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1042 with similar goals to this module. The one I have found which is most
1043 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1044 creates are very different from this modules.
1048 All complex software has bugs lurking in it, and this module is no
1049 exception. If you find a bug please either email me, or add the bug
1052 =head1 ACKNOWLEDGEMENTS
1058 Thanks to Rob for actually getting the development of this module kick-started.
1064 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1066 B<with contributions from:>
1068 Brandon (blblack) Black
1070 Florian (rafl) Ragwitz
1072 Guillermo (groditi) Roditi
1076 Rob (robkinyon) Kinyon
1078 Yuval (nothingmuch) Kogman
1080 Scott (konobi) McWhirter
1082 =head1 COPYRIGHT AND LICENSE
1084 Copyright 2006-2009 by Infinity Interactive, Inc.
1086 L<http://www.iinteractive.com>
1088 This library is free software; you can redistribute it and/or modify
1089 it under the same terms as Perl itself.