12 use Scalar::Util 'weaken';
15 use Class::MOP::Class;
16 use Class::MOP::Attribute;
17 use Class::MOP::Method;
19 use Class::MOP::Immutable;
22 *IS_RUNNING_ON_5_10 = ($] < 5.009_005)
26 *HAVE_ISAREV = defined(&mro::get_isarev)
30 # this is either part of core or set up appropriately by MRO::Compat
31 *check_package_cache_flag = \&mro::get_pkg_gen;
34 our $VERSION = '0.67';
35 our $XS_VERSION = $VERSION;
36 $VERSION = eval $VERSION;
37 our $AUTHORITY = 'cpan:STEVAN';
39 # after that everything is loaded, if we're allowed try to load faster XS
40 # versions of various things
41 _try_load_xs() or _load_pure_perl();
44 return if $ENV{CLASS_MOP_NO_XS};
50 # just doing this - no warnings 'redefine' - doesn't work
53 __PACKAGE__->XSLoader::load($XS_VERSION);
56 Sub::Name->import(qw(subname));
58 require Devel::GlobalDestruction;
59 Devel::GlobalDestruction->import("in_global_destruction");
64 die $e if $e && $e !~ /object version|loadable object/;
70 require Sub::Identify;
71 Sub::Identify->import('get_code_info');
73 *subname = sub { $_[1] };
74 *in_global_destruction = sub () { !1 }
79 # Metaclasses are singletons, so we cache them here.
80 # there is no need to worry about destruction though
81 # because they should die only when the program dies.
82 # After all, do package definitions even get reaped?
85 # means of accessing all the metaclasses that have
86 # been initialized thus far (for mugwumps obj browser)
87 sub get_all_metaclasses { %METAS }
88 sub get_all_metaclass_instances { values %METAS }
89 sub get_all_metaclass_names { keys %METAS }
90 sub get_metaclass_by_name { $METAS{$_[0]} }
91 sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
92 sub weaken_metaclass { weaken($METAS{$_[0]}) }
93 sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} }
94 sub remove_metaclass_by_name { $METAS{$_[0]} = undef }
97 # We only cache metaclasses, meaning instances of
98 # Class::MOP::Class. We do not cache instance of
99 # Class::MOP::Package or Class::MOP::Module. Mostly
100 # because I don't yet see a good reason to do so.
106 unless ( _is_valid_class_name($class) ) {
107 my $display = defined($class) ? $class : 'undef';
108 confess "Invalid class name ($display)";
111 # if the class is not already loaded in the symbol table..
112 unless (is_class_loaded($class)) {
114 my $file = $class . '.pm';
116 my $e = do { local $@; eval { require($file) }; $@ };
117 confess "Could not load class ($class) because : $e" if $e;
120 get_metaclass_by_name($class) || $class if defined wantarray;
123 sub _is_valid_class_name {
126 return 0 if ref($class);
127 return 0 unless defined($class);
128 return 0 unless length($class);
130 return 1 if $class =~ /^\w+(?:::\w+)*$/;
135 sub is_class_loaded {
138 return 0 if ref($class) || !defined($class) || !length($class);
140 # walk the symbol table tree to avoid autovififying
141 # \*{${main::}{"Foo::"}} == \*main::Foo::
144 foreach my $part (split('::', $class)) {
145 return 0 unless exists ${$$pack}{"${part}::"};
146 $pack = \*{${$$pack}{"${part}::"}};
149 # check for $VERSION or @ISA
150 return 1 if exists ${$$pack}{VERSION}
151 && defined *{${$$pack}{VERSION}}{SCALAR};
152 return 1 if exists ${$$pack}{ISA}
153 && defined *{${$$pack}{ISA}}{ARRAY};
155 # check for any method
156 foreach ( keys %{$$pack} ) {
157 next if substr($_, -2, 2) eq '::';
159 my $glob = ${$$pack}{$_} || next;
162 if ( IS_RUNNING_ON_5_10 ) {
163 return 1 if ref $glob eq 'SCALAR';
166 return 1 if defined *{$glob}{CODE};
174 ## ----------------------------------------------------------------------------
175 ## Setting up our environment ...
176 ## ----------------------------------------------------------------------------
177 ## Class::MOP needs to have a few things in the global perl environment so
178 ## that it can operate effectively. Those things are done here.
179 ## ----------------------------------------------------------------------------
181 # ... nothing yet actually ;)
183 ## ----------------------------------------------------------------------------
185 ## ----------------------------------------------------------------------------
186 ## The code below here is to bootstrap our MOP with itself. This is also
187 ## sometimes called "tying the knot". By doing this, we make it much easier
188 ## to extend the MOP through subclassing and such since now you can use the
189 ## MOP itself to extend itself.
191 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
192 ## ----------------------------------------------------------------------------
194 # We need to add in the meta-attributes here so that
195 # any subclass of Class::MOP::* will be able to
196 # inherit them using &construct_instance
198 ## --------------------------------------------------------
199 ## Class::MOP::Package
201 Class::MOP::Package->meta->add_attribute(
202 Class::MOP::Attribute->new('package' => (
204 # NOTE: we need to do this in order
205 # for the instance meta-object to
206 # not fall into meta-circular death
208 # we just alias the original method
209 # rather than re-produce it here
210 'name' => \&Class::MOP::Package::name
215 Class::MOP::Package->meta->add_attribute(
216 Class::MOP::Attribute->new('namespace' => (
219 # we just alias the original method
220 # rather than re-produce it here
221 'namespace' => \&Class::MOP::Package::namespace
224 default => sub { \undef }
228 ## --------------------------------------------------------
229 ## Class::MOP::Module
232 # yeah this is kind of stretching things a bit,
233 # but truthfully the version should be an attribute
234 # of the Module, the weirdness comes from having to
235 # stick to Perl 5 convention and store it in the
236 # $VERSION package variable. Basically if you just
237 # squint at it, it will look how you want it to look.
238 # Either as a package variable, or as a attribute of
239 # the metaclass, isn't abstraction great :)
241 Class::MOP::Module->meta->add_attribute(
242 Class::MOP::Attribute->new('version' => (
245 # we just alias the original method
246 # rather than re-produce it here
247 'version' => \&Class::MOP::Module::version
250 default => sub { \undef }
255 # By following the same conventions as version here,
256 # we are opening up the possibility that people can
257 # use the $AUTHORITY in non-Class::MOP modules as
260 Class::MOP::Module->meta->add_attribute(
261 Class::MOP::Attribute->new('authority' => (
264 # we just alias the original method
265 # rather than re-produce it here
266 'authority' => \&Class::MOP::Module::authority
269 default => sub { \undef }
273 ## --------------------------------------------------------
276 Class::MOP::Class->meta->add_attribute(
277 Class::MOP::Attribute->new('attributes' => (
279 # NOTE: we need to do this in order
280 # for the instance meta-object to
281 # not fall into meta-circular death
283 # we just alias the original method
284 # rather than re-produce it here
285 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
287 default => sub { {} }
291 Class::MOP::Class->meta->add_attribute(
292 Class::MOP::Attribute->new('methods' => (
295 # we just alias the original method
296 # rather than re-produce it here
297 'get_method_map' => \&Class::MOP::Class::get_method_map
299 default => sub { {} }
303 Class::MOP::Class->meta->add_attribute(
304 Class::MOP::Attribute->new('superclasses' => (
307 # we just alias the original method
308 # rather than re-produce it here
309 'superclasses' => \&Class::MOP::Class::superclasses
312 default => sub { \undef }
316 Class::MOP::Class->meta->add_attribute(
317 Class::MOP::Attribute->new('attribute_metaclass' => (
320 # we just alias the original method
321 # rather than re-produce it here
322 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
324 default => 'Class::MOP::Attribute',
328 Class::MOP::Class->meta->add_attribute(
329 Class::MOP::Attribute->new('method_metaclass' => (
332 # we just alias the original method
333 # rather than re-produce it here
334 'method_metaclass' => \&Class::MOP::Class::method_metaclass
336 default => 'Class::MOP::Method',
340 Class::MOP::Class->meta->add_attribute(
341 Class::MOP::Attribute->new('instance_metaclass' => (
343 # NOTE: we need to do this in order
344 # for the instance meta-object to
345 # not fall into meta-circular death
347 # we just alias the original method
348 # rather than re-produce it here
349 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
351 default => 'Class::MOP::Instance',
356 # we don't actually need to tie the knot with
357 # Class::MOP::Class here, it is actually handled
358 # within Class::MOP::Class itself in the
359 # construct_class_instance method.
361 ## --------------------------------------------------------
362 ## Class::MOP::Attribute
364 Class::MOP::Attribute->meta->add_attribute(
365 Class::MOP::Attribute->new('name' => (
367 # NOTE: we need to do this in order
368 # for the instance meta-object to
369 # not fall into meta-circular death
371 # we just alias the original method
372 # rather than re-produce it here
373 'name' => \&Class::MOP::Attribute::name
378 Class::MOP::Attribute->meta->add_attribute(
379 Class::MOP::Attribute->new('associated_class' => (
381 # NOTE: we need to do this in order
382 # for the instance meta-object to
383 # not fall into meta-circular death
385 # we just alias the original method
386 # rather than re-produce it here
387 'associated_class' => \&Class::MOP::Attribute::associated_class
392 Class::MOP::Attribute->meta->add_attribute(
393 Class::MOP::Attribute->new('accessor' => (
394 reader => { 'accessor' => \&Class::MOP::Attribute::accessor },
395 predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
399 Class::MOP::Attribute->meta->add_attribute(
400 Class::MOP::Attribute->new('reader' => (
401 reader => { 'reader' => \&Class::MOP::Attribute::reader },
402 predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
406 Class::MOP::Attribute->meta->add_attribute(
407 Class::MOP::Attribute->new('initializer' => (
408 reader => { 'initializer' => \&Class::MOP::Attribute::initializer },
409 predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
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 ## --------------------------------------------------------
530 ## Class::MOP::Method::Accessor
532 Class::MOP::Method::Accessor->meta->add_attribute(
533 Class::MOP::Attribute->new('attribute' => (
535 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
540 Class::MOP::Method::Accessor->meta->add_attribute(
541 Class::MOP::Attribute->new('accessor_type' => (
542 reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
546 ## --------------------------------------------------------
547 ## Class::MOP::Method::Constructor
549 Class::MOP::Method::Constructor->meta->add_attribute(
550 Class::MOP::Attribute->new('options' => (
552 'options' => \&Class::MOP::Method::Constructor::options
554 default => sub { +{} }
558 Class::MOP::Method::Constructor->meta->add_attribute(
559 Class::MOP::Attribute->new('associated_metaclass' => (
560 init_arg => "metaclass", # FIXME alias and rename
562 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
567 ## --------------------------------------------------------
568 ## Class::MOP::Instance
571 # these don't yet do much of anything, but are just
572 # included for completeness
574 Class::MOP::Instance->meta->add_attribute(
575 Class::MOP::Attribute->new('associated_metaclass',
576 reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
580 Class::MOP::Instance->meta->add_attribute(
581 Class::MOP::Attribute->new('_class_name',
583 reader => { _class_name => \&Class::MOP::Instance::_class_name },
584 #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway
585 #default => sub { $_[0]->associated_metaclass->name },
589 Class::MOP::Instance->meta->add_attribute(
590 Class::MOP::Attribute->new('attributes',
591 reader => { attributes => \&Class::MOP::Instance::get_all_attributes },
595 Class::MOP::Instance->meta->add_attribute(
596 Class::MOP::Attribute->new('slots',
597 reader => { slots => \&Class::MOP::Instance::slots },
601 Class::MOP::Instance->meta->add_attribute(
602 Class::MOP::Attribute->new('slot_hash',
603 reader => { slot_hash => \&Class::MOP::Instance::slot_hash },
608 # we need the meta instance of the meta instance to be created now, in order
609 # for the constructor to be able to use it
610 Class::MOP::Instance->meta->get_meta_instance;
612 # pretend the add_method never happenned. it hasn't yet affected anything
613 undef Class::MOP::Instance->meta->{_package_cache_flag};
615 ## --------------------------------------------------------
616 ## Now close all the Class::MOP::* classes
619 # we don't need to inline the
620 # constructors or the accessors
621 # this only lengthens the compile
622 # time of the MOP, and gives us
623 # no actual benefits.
625 $_->meta->make_immutable(
626 inline_constructor => 1,
627 replace_constructor => 1,
628 constructor_name => "_new",
629 inline_accessors => 0,
635 Class::MOP::Attribute
641 Class::MOP::Method::Generated
643 Class::MOP::Method::Accessor
644 Class::MOP::Method::Constructor
645 Class::MOP::Method::Wrapped
656 Class::MOP - A Meta Object Protocol for Perl 5
660 This module is a fully functioning meta object protocol for the
661 Perl 5 object system. It makes no attempt to change the behavior or
662 characteristics of the Perl 5 object system, only to create a
663 protocol for its manipulation and introspection.
665 That said, it does attempt to create the tools for building a rich
666 set of extensions to the Perl 5 object system. Every attempt has been
667 made for these tools to keep to the spirit of the Perl 5 object
668 system that we all know and love.
670 This documentation is admittedly sparse on details, as time permits
671 I will try to improve them. For now, I suggest looking at the items
672 listed in the L<SEE ALSO> section for more information. In particular
673 the book "The Art of the Meta Object Protocol" was very influential
674 in the development of this system.
676 =head2 What is a Meta Object Protocol?
678 A meta object protocol is an API to an object system.
680 To be more specific, it is a set of abstractions of the components of
681 an object system (typically things like; classes, object, methods,
682 object attributes, etc.). These abstractions can then be used to both
683 inspect and manipulate the object system which they describe.
685 It can be said that there are two MOPs for any object system; the
686 implicit MOP, and the explicit MOP. The implicit MOP handles things
687 like method dispatch or inheritance, which happen automatically as
688 part of how the object system works. The explicit MOP typically
689 handles the introspection/reflection features of the object system.
690 All object systems have implicit MOPs, without one, they would not
691 work. Explict MOPs however as less common, and depending on the
692 language can vary from restrictive (Reflection in Java or C#) to
693 wide open (CLOS is a perfect example).
695 =head2 Yet Another Class Builder!! Why?
697 This is B<not> a class builder so much as it is a I<class builder
698 B<builder>>. My intent is that an end user does not use this module
699 directly, but instead this module is used by module authors to
700 build extensions and features onto the Perl 5 object system.
702 =head2 Who is this module for?
704 This module is specifically for anyone who has ever created or
705 wanted to create a module for the Class:: namespace. The tools which
706 this module will provide will hopefully make it easier to do more
707 complex things with Perl 5 classes by removing such barriers as
708 the need to hack the symbol tables, or understand the fine details
711 =head2 What changes do I have to make to use this module?
713 This module was designed to be as unintrusive as possible. Many of
714 its features are accessible without B<any> change to your existsing
715 code at all. It is meant to be a compliment to your existing code and
716 not an intrusion on your code base. Unlike many other B<Class::>
717 modules, this module B<does not> require you subclass it, or even that
718 you C<use> it in within your module's package.
720 The only features which requires additions to your code are the
721 attribute handling and instance construction features, and these are
722 both completely optional features. The only reason for this is because
723 Perl 5's object system does not actually have these features built
724 in. More information about this feature can be found below.
726 =head2 A Note about Performance?
728 It is a common misconception that explict MOPs are performance drains.
729 But this is not a universal truth at all, it is an side-effect of
730 specific implementations. For instance, using Java reflection is much
731 slower because the JVM cannot take advantage of any compiler
732 optimizations, and the JVM has to deal with much more runtime type
733 information as well. Reflection in C# is marginally better as it was
734 designed into the language and runtime (the CLR). In contrast, CLOS
735 (the Common Lisp Object System) was built to support an explicit MOP,
736 and so performance is tuned for it.
738 This library in particular does it's absolute best to avoid putting
739 B<any> drain at all upon your code's performance. In fact, by itself
740 it does nothing to affect your existing code. So you only pay for
741 what you actually use.
743 =head2 About Metaclass compatibility
745 This module makes sure that all metaclasses created are both upwards
746 and downwards compatible. The topic of metaclass compatibility is
747 highly esoteric and is something only encountered when doing deep and
748 involved metaclass hacking. There are two basic kinds of metaclass
749 incompatibility; upwards and downwards.
751 Upwards metaclass compatibility means that the metaclass of a
752 given class is either the same as (or a subclass of) all of the
755 Downward metaclass compatibility means that the metaclasses of a
756 given class's anscestors are all either the same as (or a subclass
759 Here is a diagram showing a set of two classes (C<A> and C<B>) and
760 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
761 metaclass compatibility both upwards and downwards.
763 +---------+ +---------+
764 | Meta::A |<----| Meta::B | <....... (instance of )
765 +---------+ +---------+ <------- (inherits from)
768 +---------+ +---------+
770 +---------+ +---------+
772 As I said this is a highly esoteric topic and one you will only run
773 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
774 are interested in why this is an issue see the paper
775 I<Uniform and safe metaclass composition> linked to in the
776 L<SEE ALSO> section of this document.
778 =head2 Using custom metaclasses
780 Always use the metaclass pragma when using a custom metaclass, this
781 will ensure the proper initialization order and not accidentely
782 create an incorrect type of metaclass for you. This is a very rare
783 problem, and one which can only occur if you are doing deep metaclass
784 programming. So in other words, don't worry about it.
788 The protocol is divided into 4 main sub-protocols:
792 =item The Class protocol
794 This provides a means of manipulating and introspecting a Perl 5
795 class. It handles all of symbol table hacking for you, and provides
796 a rich set of methods that go beyond simple package introspection.
798 See L<Class::MOP::Class> for more details.
800 =item The Attribute protocol
802 This provides a consistent represenation for an attribute of a
803 Perl 5 class. Since there are so many ways to create and handle
804 attributes in Perl 5 OO, this attempts to provide as much of a
805 unified approach as possible, while giving the freedom and
806 flexibility to subclass for specialization.
808 See L<Class::MOP::Attribute> for more details.
810 =item The Method protocol
812 This provides a means of manipulating and introspecting methods in
813 the Perl 5 object system. As with attributes, there are many ways to
814 approach this topic, so we try to keep it pretty basic, while still
815 making it possible to extend the system in many ways.
817 See L<Class::MOP::Method> for more details.
819 =item The Instance protocol
821 This provides a layer of abstraction for creating object instances.
822 Since the other layers use this protocol, it is relatively easy to
823 change the type of your instances from the default HASH ref to other
824 types of references. Several examples are provided in the F<examples/>
825 directory included in this distribution.
827 See L<Class::MOP::Instance> for more details.
837 =item I<IS_RUNNING_ON_5_10>
839 We set this constant depending on what version perl we are on, this
840 allows us to take advantage of new 5.10 features and stay backwards
845 Whether or not C<mro> provides C<get_isarev>, a much faster way to get all the
846 subclasses of a certain class.
850 =head2 Utility functions
852 Note that these are all called as B<functions, not methods>.
856 =item B<load_class ($class_name)>
858 This will load a given C<$class_name> and if it does not have an
859 already initialized metaclass, then it will intialize one for it.
860 This function can be used in place of tricks like
861 C<eval "use $module"> or using C<require>.
863 =item B<is_class_loaded ($class_name)>
865 This will return a boolean depending on if the C<$class_name> has
868 NOTE: This does a basic check of the symbol table to try and
869 determine as best it can if the C<$class_name> is loaded, it
870 is probably correct about 99% of the time.
872 =item B<check_package_cache_flag ($pkg)>
874 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
876 This will return an integer that is managed by C<Class::MOP::Class>
877 to determine if a module's symbol table has been altered.
879 In Perl 5.10 or greater, this flag is package specific. However in
880 versions prior to 5.10, this will use the C<PL_sub_generation> variable
881 which is not package specific.
883 =item B<get_code_info ($code)>
885 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
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 detemine where a given C<$code> reference is from.
891 =item B<subname ($name, $code)>
893 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
895 If possible, we will load the L<Sub::Name> module and this will function
896 as C<Sub::Name::subname> does, otherwise it will just return the C<$code>
899 =item B<in_global_destruction>
901 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
903 If L<Devel::GlobalDestruction> is available, this returns true under global
906 Otherwise it's a constant returning false.
910 =head2 Metaclass cache functions
912 Class::MOP holds a cache of metaclasses, the following are functions
913 (B<not methods>) which can be used to access that cache. It is not
914 recommended that you mess with this, bad things could happen. But if
915 you are brave and willing to risk it, go for it.
919 =item B<get_all_metaclasses>
921 This will return an hash of all the metaclass instances that have
922 been cached by B<Class::MOP::Class> keyed by the package name.
924 =item B<get_all_metaclass_instances>
926 This will return an array of all the metaclass instances that have
927 been cached by B<Class::MOP::Class>.
929 =item B<get_all_metaclass_names>
931 This will return an array of all the metaclass names that have
932 been cached by B<Class::MOP::Class>.
934 =item B<get_metaclass_by_name ($name)>
936 This will return a cached B<Class::MOP::Class> instance of nothing
937 if no metaclass exist by that C<$name>.
939 =item B<store_metaclass_by_name ($name, $meta)>
941 This will store a metaclass in the cache at the supplied C<$key>.
943 =item B<weaken_metaclass ($name)>
945 In rare cases it is desireable to store a weakened reference in
946 the metaclass cache. This function will weaken the reference to
947 the metaclass stored in C<$name>.
949 =item B<does_metaclass_exist ($name)>
951 This will return true of there exists a metaclass stored in the
952 C<$name> key and return false otherwise.
954 =item B<remove_metaclass_by_name ($name)>
956 This will remove a the metaclass stored in the C<$name> key.
964 There are very few books out on Meta Object Protocols and Metaclasses
965 because it is such an esoteric topic. The following books are really
966 the only ones I have found. If you know of any more, B<I<please>>
967 email me and let me know, I would love to hear about them.
971 =item "The Art of the Meta Object Protocol"
973 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
975 =item "Putting MetaClasses to Work"
977 =item "Smalltalk: The Language"
985 =item Uniform and safe metaclass composition
987 An excellent paper by the people who brought us the original Traits paper.
988 This paper is on how Traits can be used to do safe metaclass composition,
989 and offers an excellent introduction section which delves into the topic of
990 metaclass compatibility.
992 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
994 =item Safe Metaclass Programming
996 This paper seems to precede the above paper, and propose a mix-in based
997 approach as opposed to the Traits based approach. Both papers have similar
998 information on the metaclass compatibility problem space.
1000 L<http://citeseer.ist.psu.edu/37617.html>
1008 =item The Perl 6 MetaModel work in the Pugs project
1012 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
1014 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
1024 =item CPAN Module Review of Class::MOP
1026 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1030 =head1 SIMILAR MODULES
1032 As I have said above, this module is a class-builder-builder, so it is
1033 not the same thing as modules like L<Class::Accessor> and
1034 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1035 with similar goals to this module. The one I have found which is most
1036 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1037 creates are very different from this modules.
1041 All complex software has bugs lurking in it, and this module is no
1042 exception. If you find a bug please either email me, or add the bug
1045 =head1 ACKNOWLEDGEMENTS
1051 Thanks to Rob for actually getting the development of this module kick-started.
1057 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1059 B<with contributions from:>
1061 Brandon (blblack) Black
1063 Florian (rafl) Ragwitz
1065 Guillermo (groditi) Roditi
1069 Rob (robkinyon) Kinyon
1071 Yuval (nothingmuch) Kogman
1073 Scott (konobi) McWhirter
1075 =head1 COPYRIGHT AND LICENSE
1077 Copyright 2006-2008 by Infinity Interactive, Inc.
1079 L<http://www.iinteractive.com>
1081 This library is free software; you can redistribute it and/or modify
1082 it under the same terms as Perl itself.