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.
103 sub load_one_class_of {
104 use List::Util qw/first/;
107 foreach my $class (@classes) {
108 unless ( _is_valid_class_name($class) ) {
109 my $display = defined($class) ? $class : 'undef';
110 confess "Invalid class name ($display)";
116 return $_ if is_class_loaded($_);
118 my $file = $_ . '.pm';
120 my $e = do { local $@; eval { require($file) }; $@ };
122 $exceptions{$_} = $e;
131 return get_metaclass_by_name($name) || $name;
134 # Could load no classes.
136 map { sprintf("Could not load class (%s) because : %s", $_, $exceptions{$_}) } @classes
137 ) if keys %exceptions;
141 load_one_class_of($_[0]);
144 sub _is_valid_class_name {
147 return 0 if ref($class);
148 return 0 unless defined($class);
149 return 0 unless length($class);
151 return 1 if $class =~ /^\w+(?:::\w+)*$/;
156 sub is_class_loaded {
159 return 0 if ref($class) || !defined($class) || !length($class);
161 # walk the symbol table tree to avoid autovififying
162 # \*{${main::}{"Foo::"}} == \*main::Foo::
165 foreach my $part (split('::', $class)) {
166 return 0 unless exists ${$$pack}{"${part}::"};
167 $pack = \*{${$$pack}{"${part}::"}};
170 # check for $VERSION or @ISA
171 return 1 if exists ${$$pack}{VERSION}
172 && defined *{${$$pack}{VERSION}}{SCALAR};
173 return 1 if exists ${$$pack}{ISA}
174 && defined *{${$$pack}{ISA}}{ARRAY};
176 # check for any method
177 foreach ( keys %{$$pack} ) {
178 next if substr($_, -2, 2) eq '::';
180 my $glob = ${$$pack}{$_} || next;
183 if ( IS_RUNNING_ON_5_10 ) {
184 return 1 if ref $glob eq 'SCALAR';
187 return 1 if defined *{$glob}{CODE};
195 ## ----------------------------------------------------------------------------
196 ## Setting up our environment ...
197 ## ----------------------------------------------------------------------------
198 ## Class::MOP needs to have a few things in the global perl environment so
199 ## that it can operate effectively. Those things are done here.
200 ## ----------------------------------------------------------------------------
202 # ... nothing yet actually ;)
204 ## ----------------------------------------------------------------------------
206 ## ----------------------------------------------------------------------------
207 ## The code below here is to bootstrap our MOP with itself. This is also
208 ## sometimes called "tying the knot". By doing this, we make it much easier
209 ## to extend the MOP through subclassing and such since now you can use the
210 ## MOP itself to extend itself.
212 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
213 ## ----------------------------------------------------------------------------
215 # We need to add in the meta-attributes here so that
216 # any subclass of Class::MOP::* will be able to
217 # inherit them using &construct_instance
219 ## --------------------------------------------------------
220 ## Class::MOP::Package
222 Class::MOP::Package->meta->add_attribute(
223 Class::MOP::Attribute->new('package' => (
225 # NOTE: we need to do this in order
226 # for the instance meta-object to
227 # not fall into meta-circular death
229 # we just alias the original method
230 # rather than re-produce it here
231 'name' => \&Class::MOP::Package::name
236 Class::MOP::Package->meta->add_attribute(
237 Class::MOP::Attribute->new('namespace' => (
240 # we just alias the original method
241 # rather than re-produce it here
242 'namespace' => \&Class::MOP::Package::namespace
245 default => sub { \undef }
249 ## --------------------------------------------------------
250 ## Class::MOP::Module
253 # yeah this is kind of stretching things a bit,
254 # but truthfully the version should be an attribute
255 # of the Module, the weirdness comes from having to
256 # stick to Perl 5 convention and store it in the
257 # $VERSION package variable. Basically if you just
258 # squint at it, it will look how you want it to look.
259 # Either as a package variable, or as a attribute of
260 # the metaclass, isn't abstraction great :)
262 Class::MOP::Module->meta->add_attribute(
263 Class::MOP::Attribute->new('version' => (
266 # we just alias the original method
267 # rather than re-produce it here
268 'version' => \&Class::MOP::Module::version
271 default => sub { \undef }
276 # By following the same conventions as version here,
277 # we are opening up the possibility that people can
278 # use the $AUTHORITY in non-Class::MOP modules as
281 Class::MOP::Module->meta->add_attribute(
282 Class::MOP::Attribute->new('authority' => (
285 # we just alias the original method
286 # rather than re-produce it here
287 'authority' => \&Class::MOP::Module::authority
290 default => sub { \undef }
294 ## --------------------------------------------------------
297 Class::MOP::Class->meta->add_attribute(
298 Class::MOP::Attribute->new('attributes' => (
300 # NOTE: we need to do this in order
301 # for the instance meta-object to
302 # not fall into meta-circular death
304 # we just alias the original method
305 # rather than re-produce it here
306 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
308 default => sub { {} }
312 Class::MOP::Class->meta->add_attribute(
313 Class::MOP::Attribute->new('methods' => (
316 # we just alias the original method
317 # rather than re-produce it here
318 'get_method_map' => \&Class::MOP::Class::get_method_map
320 default => sub { {} }
324 Class::MOP::Class->meta->add_attribute(
325 Class::MOP::Attribute->new('superclasses' => (
328 # we just alias the original method
329 # rather than re-produce it here
330 'superclasses' => \&Class::MOP::Class::superclasses
333 default => sub { \undef }
337 Class::MOP::Class->meta->add_attribute(
338 Class::MOP::Attribute->new('attribute_metaclass' => (
341 # we just alias the original method
342 # rather than re-produce it here
343 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
345 default => 'Class::MOP::Attribute',
349 Class::MOP::Class->meta->add_attribute(
350 Class::MOP::Attribute->new('method_metaclass' => (
353 # we just alias the original method
354 # rather than re-produce it here
355 'method_metaclass' => \&Class::MOP::Class::method_metaclass
357 default => 'Class::MOP::Method',
361 Class::MOP::Class->meta->add_attribute(
362 Class::MOP::Attribute->new('instance_metaclass' => (
364 # NOTE: we need to do this in order
365 # for the instance meta-object to
366 # not fall into meta-circular death
368 # we just alias the original method
369 # rather than re-produce it here
370 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
372 default => 'Class::MOP::Instance',
377 # we don't actually need to tie the knot with
378 # Class::MOP::Class here, it is actually handled
379 # within Class::MOP::Class itself in the
380 # construct_class_instance method.
382 ## --------------------------------------------------------
383 ## Class::MOP::Attribute
385 Class::MOP::Attribute->meta->add_attribute(
386 Class::MOP::Attribute->new('name' => (
388 # NOTE: we need to do this in order
389 # for the instance meta-object to
390 # not fall into meta-circular death
392 # we just alias the original method
393 # rather than re-produce it here
394 'name' => \&Class::MOP::Attribute::name
399 Class::MOP::Attribute->meta->add_attribute(
400 Class::MOP::Attribute->new('associated_class' => (
402 # NOTE: we need to do this in order
403 # for the instance meta-object to
404 # not fall into meta-circular death
406 # we just alias the original method
407 # rather than re-produce it here
408 'associated_class' => \&Class::MOP::Attribute::associated_class
413 Class::MOP::Attribute->meta->add_attribute(
414 Class::MOP::Attribute->new('accessor' => (
415 reader => { 'accessor' => \&Class::MOP::Attribute::accessor },
416 predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
420 Class::MOP::Attribute->meta->add_attribute(
421 Class::MOP::Attribute->new('reader' => (
422 reader => { 'reader' => \&Class::MOP::Attribute::reader },
423 predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
427 Class::MOP::Attribute->meta->add_attribute(
428 Class::MOP::Attribute->new('initializer' => (
429 reader => { 'initializer' => \&Class::MOP::Attribute::initializer },
430 predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
434 Class::MOP::Attribute->meta->add_attribute(
435 Class::MOP::Attribute->new('writer' => (
436 reader => { 'writer' => \&Class::MOP::Attribute::writer },
437 predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
441 Class::MOP::Attribute->meta->add_attribute(
442 Class::MOP::Attribute->new('predicate' => (
443 reader => { 'predicate' => \&Class::MOP::Attribute::predicate },
444 predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate },
448 Class::MOP::Attribute->meta->add_attribute(
449 Class::MOP::Attribute->new('clearer' => (
450 reader => { 'clearer' => \&Class::MOP::Attribute::clearer },
451 predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer },
455 Class::MOP::Attribute->meta->add_attribute(
456 Class::MOP::Attribute->new('builder' => (
457 reader => { 'builder' => \&Class::MOP::Attribute::builder },
458 predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder },
462 Class::MOP::Attribute->meta->add_attribute(
463 Class::MOP::Attribute->new('init_arg' => (
464 reader => { 'init_arg' => \&Class::MOP::Attribute::init_arg },
465 predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg },
469 Class::MOP::Attribute->meta->add_attribute(
470 Class::MOP::Attribute->new('default' => (
471 # default has a custom 'reader' method ...
472 predicate => { 'has_default' => \&Class::MOP::Attribute::has_default },
476 Class::MOP::Attribute->meta->add_attribute(
477 Class::MOP::Attribute->new('associated_methods' => (
478 reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods },
479 default => sub { [] }
483 Class::MOP::Attribute->meta->add_method('clone' => sub {
485 $self->meta->clone_object($self, @_);
488 ## --------------------------------------------------------
489 ## Class::MOP::Method
490 Class::MOP::Method->meta->add_attribute(
491 Class::MOP::Attribute->new('body' => (
492 reader => { 'body' => \&Class::MOP::Method::body },
496 Class::MOP::Method->meta->add_attribute(
497 Class::MOP::Attribute->new('associated_metaclass' => (
498 reader => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass },
502 Class::MOP::Method->meta->add_attribute(
503 Class::MOP::Attribute->new('package_name' => (
504 reader => { 'package_name' => \&Class::MOP::Method::package_name },
508 Class::MOP::Method->meta->add_attribute(
509 Class::MOP::Attribute->new('name' => (
510 reader => { 'name' => \&Class::MOP::Method::name },
514 Class::MOP::Method->meta->add_attribute(
515 Class::MOP::Attribute->new('original_method' => (
516 reader => { 'original_method' => \&Class::MOP::Method::original_method },
517 writer => { '_set_original_method' => \&Class::MOP::Method::_set_original_method },
521 Class::MOP::Method->meta->add_method('clone' => sub {
523 my $clone = $self->meta->clone_object($self, @_);
524 $clone->_set_original_method($self);
528 ## --------------------------------------------------------
529 ## Class::MOP::Method::Wrapped
532 # the way this item is initialized, this
533 # really does not follow the standard
534 # practices of attributes, but we put
535 # it here for completeness
536 Class::MOP::Method::Wrapped->meta->add_attribute(
537 Class::MOP::Attribute->new('modifier_table')
540 ## --------------------------------------------------------
541 ## Class::MOP::Method::Generated
543 Class::MOP::Method::Generated->meta->add_attribute(
544 Class::MOP::Attribute->new('is_inline' => (
545 reader => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline },
550 ## --------------------------------------------------------
551 ## Class::MOP::Method::Accessor
553 Class::MOP::Method::Accessor->meta->add_attribute(
554 Class::MOP::Attribute->new('attribute' => (
556 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
561 Class::MOP::Method::Accessor->meta->add_attribute(
562 Class::MOP::Attribute->new('accessor_type' => (
563 reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
567 ## --------------------------------------------------------
568 ## Class::MOP::Method::Constructor
570 Class::MOP::Method::Constructor->meta->add_attribute(
571 Class::MOP::Attribute->new('options' => (
573 'options' => \&Class::MOP::Method::Constructor::options
575 default => sub { +{} }
579 Class::MOP::Method::Constructor->meta->add_attribute(
580 Class::MOP::Attribute->new('associated_metaclass' => (
581 init_arg => "metaclass", # FIXME alias and rename
583 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
588 ## --------------------------------------------------------
589 ## Class::MOP::Instance
592 # these don't yet do much of anything, but are just
593 # included for completeness
595 Class::MOP::Instance->meta->add_attribute(
596 Class::MOP::Attribute->new('associated_metaclass',
597 reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
601 Class::MOP::Instance->meta->add_attribute(
602 Class::MOP::Attribute->new('_class_name',
604 reader => { _class_name => \&Class::MOP::Instance::_class_name },
605 #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway
606 #default => sub { $_[0]->associated_metaclass->name },
610 Class::MOP::Instance->meta->add_attribute(
611 Class::MOP::Attribute->new('attributes',
612 reader => { attributes => \&Class::MOP::Instance::get_all_attributes },
616 Class::MOP::Instance->meta->add_attribute(
617 Class::MOP::Attribute->new('slots',
618 reader => { slots => \&Class::MOP::Instance::slots },
622 Class::MOP::Instance->meta->add_attribute(
623 Class::MOP::Attribute->new('slot_hash',
624 reader => { slot_hash => \&Class::MOP::Instance::slot_hash },
629 # we need the meta instance of the meta instance to be created now, in order
630 # for the constructor to be able to use it
631 Class::MOP::Instance->meta->get_meta_instance;
633 # pretend the add_method never happenned. it hasn't yet affected anything
634 undef Class::MOP::Instance->meta->{_package_cache_flag};
636 ## --------------------------------------------------------
637 ## Now close all the Class::MOP::* classes
640 # we don't need to inline the
641 # constructors or the accessors
642 # this only lengthens the compile
643 # time of the MOP, and gives us
644 # no actual benefits.
646 $_->meta->make_immutable(
647 inline_constructor => 1,
648 replace_constructor => 1,
649 constructor_name => "_new",
650 inline_accessors => 0,
656 Class::MOP::Attribute
662 Class::MOP::Method::Generated
664 Class::MOP::Method::Accessor
665 Class::MOP::Method::Constructor
666 Class::MOP::Method::Wrapped
677 Class::MOP - A Meta Object Protocol for Perl 5
681 This module is a fully functioning meta object protocol for the
682 Perl 5 object system. It makes no attempt to change the behavior or
683 characteristics of the Perl 5 object system, only to create a
684 protocol for its manipulation and introspection.
686 That said, it does attempt to create the tools for building a rich
687 set of extensions to the Perl 5 object system. Every attempt has been
688 made for these tools to keep to the spirit of the Perl 5 object
689 system that we all know and love.
691 This documentation is admittedly sparse on details, as time permits
692 I will try to improve them. For now, I suggest looking at the items
693 listed in the L<SEE ALSO> section for more information. In particular
694 the book "The Art of the Meta Object Protocol" was very influential
695 in the development of this system.
697 =head2 What is a Meta Object Protocol?
699 A meta object protocol is an API to an object system.
701 To be more specific, it is a set of abstractions of the components of
702 an object system (typically things like; classes, object, methods,
703 object attributes, etc.). These abstractions can then be used to both
704 inspect and manipulate the object system which they describe.
706 It can be said that there are two MOPs for any object system; the
707 implicit MOP, and the explicit MOP. The implicit MOP handles things
708 like method dispatch or inheritance, which happen automatically as
709 part of how the object system works. The explicit MOP typically
710 handles the introspection/reflection features of the object system.
711 All object systems have implicit MOPs, without one, they would not
712 work. Explict MOPs however as less common, and depending on the
713 language can vary from restrictive (Reflection in Java or C#) to
714 wide open (CLOS is a perfect example).
716 =head2 Yet Another Class Builder!! Why?
718 This is B<not> a class builder so much as it is a I<class builder
719 B<builder>>. My intent is that an end user does not use this module
720 directly, but instead this module is used by module authors to
721 build extensions and features onto the Perl 5 object system.
723 =head2 Who is this module for?
725 This module is specifically for anyone who has ever created or
726 wanted to create a module for the Class:: namespace. The tools which
727 this module will provide will hopefully make it easier to do more
728 complex things with Perl 5 classes by removing such barriers as
729 the need to hack the symbol tables, or understand the fine details
732 =head2 What changes do I have to make to use this module?
734 This module was designed to be as unintrusive as possible. Many of
735 its features are accessible without B<any> change to your existsing
736 code at all. It is meant to be a compliment to your existing code and
737 not an intrusion on your code base. Unlike many other B<Class::>
738 modules, this module B<does not> require you subclass it, or even that
739 you C<use> it in within your module's package.
741 The only features which requires additions to your code are the
742 attribute handling and instance construction features, and these are
743 both completely optional features. The only reason for this is because
744 Perl 5's object system does not actually have these features built
745 in. More information about this feature can be found below.
747 =head2 A Note about Performance?
749 It is a common misconception that explict MOPs are performance drains.
750 But this is not a universal truth at all, it is an side-effect of
751 specific implementations. For instance, using Java reflection is much
752 slower because the JVM cannot take advantage of any compiler
753 optimizations, and the JVM has to deal with much more runtime type
754 information as well. Reflection in C# is marginally better as it was
755 designed into the language and runtime (the CLR). In contrast, CLOS
756 (the Common Lisp Object System) was built to support an explicit MOP,
757 and so performance is tuned for it.
759 This library in particular does it's absolute best to avoid putting
760 B<any> drain at all upon your code's performance. In fact, by itself
761 it does nothing to affect your existing code. So you only pay for
762 what you actually use.
764 =head2 About Metaclass compatibility
766 This module makes sure that all metaclasses created are both upwards
767 and downwards compatible. The topic of metaclass compatibility is
768 highly esoteric and is something only encountered when doing deep and
769 involved metaclass hacking. There are two basic kinds of metaclass
770 incompatibility; upwards and downwards.
772 Upwards metaclass compatibility means that the metaclass of a
773 given class is either the same as (or a subclass of) all of the
776 Downward metaclass compatibility means that the metaclasses of a
777 given class's anscestors are all either the same as (or a subclass
780 Here is a diagram showing a set of two classes (C<A> and C<B>) and
781 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
782 metaclass compatibility both upwards and downwards.
784 +---------+ +---------+
785 | Meta::A |<----| Meta::B | <....... (instance of )
786 +---------+ +---------+ <------- (inherits from)
789 +---------+ +---------+
791 +---------+ +---------+
793 As I said this is a highly esoteric topic and one you will only run
794 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
795 are interested in why this is an issue see the paper
796 I<Uniform and safe metaclass composition> linked to in the
797 L<SEE ALSO> section of this document.
799 =head2 Using custom metaclasses
801 Always use the metaclass pragma when using a custom metaclass, this
802 will ensure the proper initialization order and not accidentely
803 create an incorrect type of metaclass for you. This is a very rare
804 problem, and one which can only occur if you are doing deep metaclass
805 programming. So in other words, don't worry about it.
809 The protocol is divided into 4 main sub-protocols:
813 =item The Class protocol
815 This provides a means of manipulating and introspecting a Perl 5
816 class. It handles all of symbol table hacking for you, and provides
817 a rich set of methods that go beyond simple package introspection.
819 See L<Class::MOP::Class> for more details.
821 =item The Attribute protocol
823 This provides a consistent represenation for an attribute of a
824 Perl 5 class. Since there are so many ways to create and handle
825 attributes in Perl 5 OO, this attempts to provide as much of a
826 unified approach as possible, while giving the freedom and
827 flexibility to subclass for specialization.
829 See L<Class::MOP::Attribute> for more details.
831 =item The Method protocol
833 This provides a means of manipulating and introspecting methods in
834 the Perl 5 object system. As with attributes, there are many ways to
835 approach this topic, so we try to keep it pretty basic, while still
836 making it possible to extend the system in many ways.
838 See L<Class::MOP::Method> for more details.
840 =item The Instance protocol
842 This provides a layer of abstraction for creating object instances.
843 Since the other layers use this protocol, it is relatively easy to
844 change the type of your instances from the default HASH ref to other
845 types of references. Several examples are provided in the F<examples/>
846 directory included in this distribution.
848 See L<Class::MOP::Instance> for more details.
858 =item I<IS_RUNNING_ON_5_10>
860 We set this constant depending on what version perl we are on, this
861 allows us to take advantage of new 5.10 features and stay backwards
866 Whether or not C<mro> provides C<get_isarev>, a much faster way to get all the
867 subclasses of a certain class.
871 =head2 Utility functions
873 Note that these are all called as B<functions, not methods>.
877 =item B<load_class ($class_name)>
879 This will load a given C<$class_name> and if it does not have an
880 already initialized metaclass, then it will intialize one for it.
881 This function can be used in place of tricks like
882 C<eval "use $module"> or using C<require>.
884 =item B<load_one_class_of ($class_name, [$class_name, ...])>
886 This will attempt to load the list of classes given as parameters.
887 The first class successfully found or loaded will have it's metaclass
888 initialized (if needed) and returned. Subsequent classes to the first
889 loaded class will be ignored, and an exception will be thrown if none
890 of the supplied class names can be loaded.
892 =item B<is_class_loaded ($class_name)>
894 This will return a boolean depending on if the C<$class_name> has
897 NOTE: This does a basic check of the symbol table to try and
898 determine as best it can if the C<$class_name> is loaded, it
899 is probably correct about 99% of the time.
901 =item B<check_package_cache_flag ($pkg)>
903 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
905 This will return an integer that is managed by C<Class::MOP::Class>
906 to determine if a module's symbol table has been altered.
908 In Perl 5.10 or greater, this flag is package specific. However in
909 versions prior to 5.10, this will use the C<PL_sub_generation> variable
910 which is not package specific.
912 =item B<get_code_info ($code)>
914 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
916 This function returns two values, the name of the package the C<$code>
917 is from and the name of the C<$code> itself. This is used by several
918 elements of the MOP to detemine where a given C<$code> reference is from.
920 =item B<subname ($name, $code)>
922 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
924 If possible, we will load the L<Sub::Name> module and this will function
925 as C<Sub::Name::subname> does, otherwise it will just return the C<$code>
928 =item B<in_global_destruction>
930 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
932 If L<Devel::GlobalDestruction> is available, this returns true under global
935 Otherwise it's a constant returning false.
939 =head2 Metaclass cache functions
941 Class::MOP holds a cache of metaclasses, the following are functions
942 (B<not methods>) which can be used to access that cache. It is not
943 recommended that you mess with this, bad things could happen. But if
944 you are brave and willing to risk it, go for it.
948 =item B<get_all_metaclasses>
950 This will return an hash of all the metaclass instances that have
951 been cached by B<Class::MOP::Class> keyed by the package name.
953 =item B<get_all_metaclass_instances>
955 This will return an array of all the metaclass instances that have
956 been cached by B<Class::MOP::Class>.
958 =item B<get_all_metaclass_names>
960 This will return an array of all the metaclass names that have
961 been cached by B<Class::MOP::Class>.
963 =item B<get_metaclass_by_name ($name)>
965 This will return a cached B<Class::MOP::Class> instance of nothing
966 if no metaclass exist by that C<$name>.
968 =item B<store_metaclass_by_name ($name, $meta)>
970 This will store a metaclass in the cache at the supplied C<$key>.
972 =item B<weaken_metaclass ($name)>
974 In rare cases it is desireable to store a weakened reference in
975 the metaclass cache. This function will weaken the reference to
976 the metaclass stored in C<$name>.
978 =item B<does_metaclass_exist ($name)>
980 This will return true of there exists a metaclass stored in the
981 C<$name> key and return false otherwise.
983 =item B<remove_metaclass_by_name ($name)>
985 This will remove a the metaclass stored in the C<$name> key.
993 There are very few books out on Meta Object Protocols and Metaclasses
994 because it is such an esoteric topic. The following books are really
995 the only ones I have found. If you know of any more, B<I<please>>
996 email me and let me know, I would love to hear about them.
1000 =item "The Art of the Meta Object Protocol"
1002 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
1004 =item "Putting MetaClasses to Work"
1006 =item "Smalltalk: The Language"
1014 =item Uniform and safe metaclass composition
1016 An excellent paper by the people who brought us the original Traits paper.
1017 This paper is on how Traits can be used to do safe metaclass composition,
1018 and offers an excellent introduction section which delves into the topic of
1019 metaclass compatibility.
1021 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
1023 =item Safe Metaclass Programming
1025 This paper seems to precede the above paper, and propose a mix-in based
1026 approach as opposed to the Traits based approach. Both papers have similar
1027 information on the metaclass compatibility problem space.
1029 L<http://citeseer.ist.psu.edu/37617.html>
1037 =item The Perl 6 MetaModel work in the Pugs project
1041 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
1043 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
1053 =item CPAN Module Review of Class::MOP
1055 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1059 =head1 SIMILAR MODULES
1061 As I have said above, this module is a class-builder-builder, so it is
1062 not the same thing as modules like L<Class::Accessor> and
1063 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1064 with similar goals to this module. The one I have found which is most
1065 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1066 creates are very different from this modules.
1070 All complex software has bugs lurking in it, and this module is no
1071 exception. If you find a bug please either email me, or add the bug
1074 =head1 ACKNOWLEDGEMENTS
1080 Thanks to Rob for actually getting the development of this module kick-started.
1086 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1088 B<with contributions from:>
1090 Brandon (blblack) Black
1092 Florian (rafl) Ragwitz
1094 Guillermo (groditi) Roditi
1098 Rob (robkinyon) Kinyon
1100 Yuval (nothingmuch) Kogman
1102 Scott (konobi) McWhirter
1104 =head1 COPYRIGHT AND LICENSE
1106 Copyright 2006-2008 by Infinity Interactive, Inc.
1108 L<http://www.iinteractive.com>
1110 This library is free software; you can redistribute it and/or modify
1111 it under the same terms as Perl itself.