10 use Scalar::Util 'weaken';
12 use Sub::Identify 'get_code_info';
14 use Class::MOP::Class;
15 use Class::MOP::Attribute;
16 use Class::MOP::Method;
18 use Class::MOP::Immutable;
22 our $VERSION = '0.65';
23 our $AUTHORITY = 'cpan:STEVAN';
25 *IS_RUNNING_ON_5_10 = ($] < 5.009_005)
29 *HAVE_ISAREV = defined(&mro::get_isarev)
35 eval 'use Sub::Name qw(subname); 1' || eval 'sub subname { $_[1] }';
38 # this is either part of core or set up appropriately by MRO::Compat
39 *check_package_cache_flag = \&mro::get_pkg_gen;
42 # after that everything is loaded, if we're allowed try to load faster XS
43 # versions of various things
44 unless ($ENV{CLASS_MOP_NO_XS}) {
49 __PACKAGE__->XSLoader::load(our $VERSION);
54 die $e if $e && $e !~ /object version|loadable object/;
58 # Metaclasses are singletons, so we cache them here.
59 # there is no need to worry about destruction though
60 # because they should die only when the program dies.
61 # After all, do package definitions even get reaped?
64 # means of accessing all the metaclasses that have
65 # been initialized thus far (for mugwumps obj browser)
66 sub get_all_metaclasses { %METAS }
67 sub get_all_metaclass_instances { values %METAS }
68 sub get_all_metaclass_names { keys %METAS }
69 sub get_metaclass_by_name { $METAS{$_[0]} }
70 sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
71 sub weaken_metaclass { weaken($METAS{$_[0]}) }
72 sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} }
73 sub remove_metaclass_by_name { $METAS{$_[0]} = undef }
76 # We only cache metaclasses, meaning instances of
77 # Class::MOP::Class. We do not cache instance of
78 # Class::MOP::Package or Class::MOP::Module. Mostly
79 # because I don't yet see a good reason to do so.
85 if (ref($class) || !defined($class) || !length($class)) {
86 my $display = defined($class) ? $class : 'undef';
87 confess "Invalid class name ($display)";
90 # if the class is not already loaded in the symbol table..
91 unless (is_class_loaded($class)) {
93 my $file = $class . '.pm';
95 my $e = do { local $@; eval { require($file) }; $@ };
96 confess "Could not load class ($class) because : $e" if $e;
99 # initialize a metaclass if necessary
100 unless (does_metaclass_exist($class)) {
101 my $e = do { local $@; eval { Class::MOP::Class->initialize($class) }; $@ };
102 confess "Could not initialize class ($class) because : $e" if $e;
105 return get_metaclass_by_name($class) if defined wantarray;
108 sub is_class_loaded {
111 return 0 if ref($class) || !defined($class) || !length($class);
113 # walk the symbol table tree to avoid autovififying
114 # \*{${main::}{"Foo::"}} == \*main::Foo::
117 foreach my $part (split('::', $class)) {
118 return 0 unless exists ${$$pack}{"${part}::"};
119 $pack = \*{${$$pack}{"${part}::"}};
122 # check for $VERSION or @ISA
123 return 1 if exists ${$$pack}{VERSION}
124 && defined *{${$$pack}{VERSION}}{SCALAR};
125 return 1 if exists ${$$pack}{ISA}
126 && defined *{${$$pack}{ISA}}{ARRAY};
128 # check for any method
129 foreach ( keys %{$$pack} ) {
130 next if substr($_, -2, 2) eq '::';
132 my $glob = ${$$pack}{$_} || next;
135 if ( IS_RUNNING_ON_5_10 ) {
136 return 1 if ref $glob eq 'SCALAR';
139 return 1 if defined *{$glob}{CODE};
147 ## ----------------------------------------------------------------------------
148 ## Setting up our environment ...
149 ## ----------------------------------------------------------------------------
150 ## Class::MOP needs to have a few things in the global perl environment so
151 ## that it can operate effectively. Those things are done here.
152 ## ----------------------------------------------------------------------------
154 # ... nothing yet actually ;)
156 ## ----------------------------------------------------------------------------
158 ## ----------------------------------------------------------------------------
159 ## The code below here is to bootstrap our MOP with itself. This is also
160 ## sometimes called "tying the knot". By doing this, we make it much easier
161 ## to extend the MOP through subclassing and such since now you can use the
162 ## MOP itself to extend itself.
164 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
165 ## ----------------------------------------------------------------------------
167 # We need to add in the meta-attributes here so that
168 # any subclass of Class::MOP::* will be able to
169 # inherit them using &construct_instance
171 ## --------------------------------------------------------
172 ## Class::MOP::Package
174 Class::MOP::Package->meta->add_attribute(
175 Class::MOP::Attribute->new('package' => (
177 # NOTE: we need to do this in order
178 # for the instance meta-object to
179 # not fall into meta-circular death
181 # we just alias the original method
182 # rather than re-produce it here
183 'name' => \&Class::MOP::Package::name
188 Class::MOP::Package->meta->add_attribute(
189 Class::MOP::Attribute->new('namespace' => (
192 # we just alias the original method
193 # rather than re-produce it here
194 'namespace' => \&Class::MOP::Package::namespace
197 default => sub { \undef }
202 # use the metaclass to construct the meta-package
203 # which is a superclass of the metaclass itself :P
204 Class::MOP::Package->meta->add_method('initialize' => sub {
206 my $package_name = shift;
207 $class->meta->new_object('package' => $package_name, @_);
210 ## --------------------------------------------------------
211 ## Class::MOP::Module
214 # yeah this is kind of stretching things a bit,
215 # but truthfully the version should be an attribute
216 # of the Module, the weirdness comes from having to
217 # stick to Perl 5 convention and store it in the
218 # $VERSION package variable. Basically if you just
219 # squint at it, it will look how you want it to look.
220 # Either as a package variable, or as a attribute of
221 # the metaclass, isn't abstraction great :)
223 Class::MOP::Module->meta->add_attribute(
224 Class::MOP::Attribute->new('version' => (
227 # we just alias the original method
228 # rather than re-produce it here
229 'version' => \&Class::MOP::Module::version
232 default => sub { \undef }
237 # By following the same conventions as version here,
238 # we are opening up the possibility that people can
239 # use the $AUTHORITY in non-Class::MOP modules as
242 Class::MOP::Module->meta->add_attribute(
243 Class::MOP::Attribute->new('authority' => (
246 # we just alias the original method
247 # rather than re-produce it here
248 'authority' => \&Class::MOP::Module::authority
251 default => sub { \undef }
255 ## --------------------------------------------------------
258 Class::MOP::Class->meta->add_attribute(
259 Class::MOP::Attribute->new('attributes' => (
261 # NOTE: we need to do this in order
262 # for the instance meta-object to
263 # not fall into meta-circular death
265 # we just alias the original method
266 # rather than re-produce it here
267 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
269 default => sub { {} }
273 Class::MOP::Class->meta->add_attribute(
274 Class::MOP::Attribute->new('methods' => (
277 # we just alias the original method
278 # rather than re-produce it here
279 'get_method_map' => \&Class::MOP::Class::get_method_map
281 default => sub { {} }
285 Class::MOP::Class->meta->add_attribute(
286 Class::MOP::Attribute->new('superclasses' => (
289 # we just alias the original method
290 # rather than re-produce it here
291 'superclasses' => \&Class::MOP::Class::superclasses
294 default => sub { \undef }
298 Class::MOP::Class->meta->add_attribute(
299 Class::MOP::Attribute->new('attribute_metaclass' => (
302 # we just alias the original method
303 # rather than re-produce it here
304 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
306 default => 'Class::MOP::Attribute',
310 Class::MOP::Class->meta->add_attribute(
311 Class::MOP::Attribute->new('method_metaclass' => (
314 # we just alias the original method
315 # rather than re-produce it here
316 'method_metaclass' => \&Class::MOP::Class::method_metaclass
318 default => 'Class::MOP::Method',
322 Class::MOP::Class->meta->add_attribute(
323 Class::MOP::Attribute->new('instance_metaclass' => (
325 # NOTE: we need to do this in order
326 # for the instance meta-object to
327 # not fall into meta-circular death
329 # we just alias the original method
330 # rather than re-produce it here
331 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
333 default => 'Class::MOP::Instance',
338 # we don't actually need to tie the knot with
339 # Class::MOP::Class here, it is actually handled
340 # within Class::MOP::Class itself in the
341 # construct_class_instance method.
343 ## --------------------------------------------------------
344 ## Class::MOP::Attribute
346 Class::MOP::Attribute->meta->add_attribute(
347 Class::MOP::Attribute->new('name' => (
349 # NOTE: we need to do this in order
350 # for the instance meta-object to
351 # not fall into meta-circular death
353 # we just alias the original method
354 # rather than re-produce it here
355 'name' => \&Class::MOP::Attribute::name
360 Class::MOP::Attribute->meta->add_attribute(
361 Class::MOP::Attribute->new('associated_class' => (
363 # NOTE: we need to do this in order
364 # for the instance meta-object to
365 # not fall into meta-circular death
367 # we just alias the original method
368 # rather than re-produce it here
369 'associated_class' => \&Class::MOP::Attribute::associated_class
374 Class::MOP::Attribute->meta->add_attribute(
375 Class::MOP::Attribute->new('accessor' => (
376 reader => { 'accessor' => \&Class::MOP::Attribute::accessor },
377 predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
381 Class::MOP::Attribute->meta->add_attribute(
382 Class::MOP::Attribute->new('reader' => (
383 reader => { 'reader' => \&Class::MOP::Attribute::reader },
384 predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
388 Class::MOP::Attribute->meta->add_attribute(
389 Class::MOP::Attribute->new('initializer' => (
390 reader => { 'initializer' => \&Class::MOP::Attribute::initializer },
391 predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
395 Class::MOP::Attribute->meta->add_attribute(
396 Class::MOP::Attribute->new('writer' => (
397 reader => { 'writer' => \&Class::MOP::Attribute::writer },
398 predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
402 Class::MOP::Attribute->meta->add_attribute(
403 Class::MOP::Attribute->new('predicate' => (
404 reader => { 'predicate' => \&Class::MOP::Attribute::predicate },
405 predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate },
409 Class::MOP::Attribute->meta->add_attribute(
410 Class::MOP::Attribute->new('clearer' => (
411 reader => { 'clearer' => \&Class::MOP::Attribute::clearer },
412 predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer },
416 Class::MOP::Attribute->meta->add_attribute(
417 Class::MOP::Attribute->new('builder' => (
418 reader => { 'builder' => \&Class::MOP::Attribute::builder },
419 predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder },
423 Class::MOP::Attribute->meta->add_attribute(
424 Class::MOP::Attribute->new('init_arg' => (
425 reader => { 'init_arg' => \&Class::MOP::Attribute::init_arg },
426 predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg },
430 Class::MOP::Attribute->meta->add_attribute(
431 Class::MOP::Attribute->new('default' => (
432 # default has a custom 'reader' method ...
433 predicate => { 'has_default' => \&Class::MOP::Attribute::has_default },
437 Class::MOP::Attribute->meta->add_attribute(
438 Class::MOP::Attribute->new('associated_methods' => (
439 reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods },
440 default => sub { [] }
444 # NOTE: (meta-circularity)
445 # This should be one of the last things done
446 # it will "tie the knot" with Class::MOP::Attribute
447 # so that it uses the attributes meta-objects
448 # to construct itself.
449 Class::MOP::Attribute->meta->add_method('new' => sub {
450 my ( $class, @args ) = @_;
452 unshift @args, "name" if @args % 2 == 1;
455 my $name = $options{name};
457 (defined $name && $name)
458 || confess "You must provide a name for the attribute";
459 $options{init_arg} = $name
460 if not exists $options{init_arg};
462 if(exists $options{builder}){
463 confess("builder must be a defined scalar value which is a method name")
464 if ref $options{builder} || !(defined $options{builder});
465 confess("Setting both default and builder is not allowed.")
466 if exists $options{default};
468 (Class::MOP::Attribute::is_default_a_coderef(\%options))
469 || confess("References are not allowed as default values, you must ".
470 "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])")
471 if exists $options{default} && ref $options{default};
474 # return the new object
475 $class->meta->new_object(%options);
478 Class::MOP::Attribute->meta->add_method('clone' => sub {
480 $self->meta->clone_object($self, @_);
483 ## --------------------------------------------------------
484 ## Class::MOP::Method
485 Class::MOP::Method->meta->add_attribute(
486 Class::MOP::Attribute->new('body' => (
487 reader => { 'body' => \&Class::MOP::Method::body },
491 Class::MOP::Method->meta->add_attribute(
492 Class::MOP::Attribute->new('associated_metaclass' => (
493 reader => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass },
497 Class::MOP::Method->meta->add_attribute(
498 Class::MOP::Attribute->new('package_name' => (
499 reader => { 'package_name' => \&Class::MOP::Method::package_name },
503 Class::MOP::Method->meta->add_attribute(
504 Class::MOP::Attribute->new('name' => (
505 reader => { 'name' => \&Class::MOP::Method::name },
509 # FIMXE prime candidate for immutablization
510 Class::MOP::Method->meta->add_method('wrap' => sub {
511 my ( $class, @args ) = @_;
513 unshift @args, 'body' if @args % 2 == 1;
516 my $code = $options{body};
518 ('CODE' eq ref($code))
519 || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
521 ($options{package_name} && $options{name})
522 || confess "You must supply the package_name and name parameters";
524 # return the new object
525 $class->meta->new_object(%options);
528 Class::MOP::Method->meta->add_method('clone' => sub {
530 $self->meta->clone_object($self, @_);
533 ## --------------------------------------------------------
534 ## Class::MOP::Method::Wrapped
537 # the way this item is initialized, this
538 # really does not follow the standard
539 # practices of attributes, but we put
540 # it here for completeness
541 Class::MOP::Method::Wrapped->meta->add_attribute(
542 Class::MOP::Attribute->new('modifier_table')
545 ## --------------------------------------------------------
546 ## Class::MOP::Method::Generated
548 Class::MOP::Method::Generated->meta->add_attribute(
549 Class::MOP::Attribute->new('is_inline' => (
550 reader => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline },
555 Class::MOP::Method::Generated->meta->add_method('new' => sub {
556 my ($class, %options) = @_;
557 ($options{package_name} && $options{name})
558 || confess "You must supply the package_name and name parameters";
559 my $self = $class->meta->new_object(%options);
560 $self->initialize_body;
564 ## --------------------------------------------------------
565 ## Class::MOP::Method::Accessor
567 Class::MOP::Method::Accessor->meta->add_attribute(
568 Class::MOP::Attribute->new('attribute' => (
570 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
575 Class::MOP::Method::Accessor->meta->add_attribute(
576 Class::MOP::Attribute->new('accessor_type' => (
577 reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
581 Class::MOP::Method::Accessor->meta->add_method('new' => sub {
585 (exists $options{attribute})
586 || confess "You must supply an attribute to construct with";
588 (exists $options{accessor_type})
589 || confess "You must supply an accessor_type to construct with";
591 (Scalar::Util::blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
592 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
594 ($options{package_name} && $options{name})
595 || confess "You must supply the package_name and name parameters";
597 # return the new object
598 my $self = $class->meta->new_object(%options);
600 # we don't want this creating
601 # a cycle in the code, if not
603 Scalar::Util::weaken($self->{'attribute'});
605 $self->initialize_body;
611 ## --------------------------------------------------------
612 ## Class::MOP::Method::Constructor
614 Class::MOP::Method::Constructor->meta->add_attribute(
615 Class::MOP::Attribute->new('options' => (
617 'options' => \&Class::MOP::Method::Constructor::options
619 default => sub { +{} }
623 Class::MOP::Method::Constructor->meta->add_attribute(
624 Class::MOP::Attribute->new('associated_metaclass' => (
625 init_arg => "metaclass", # FIXME alias and rename
627 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
632 Class::MOP::Method::Constructor->meta->add_method('new' => sub {
636 (Scalar::Util::blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
637 || confess "You must pass a metaclass instance if you want to inline"
638 if $options{is_inline};
640 ($options{package_name} && $options{name})
641 || confess "You must supply the package_name and name parameters";
643 # return the new object
644 my $self = $class->meta->new_object(%options);
646 # we don't want this creating
647 # a cycle in the code, if not
649 Scalar::Util::weaken($self->{'associated_metaclass'});
651 $self->initialize_body;
656 ## --------------------------------------------------------
657 ## Class::MOP::Instance
660 # these don't yet do much of anything, but are just
661 # included for completeness
663 Class::MOP::Instance->meta->add_attribute(
664 Class::MOP::Attribute->new('associated_metaclass',
665 reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
669 Class::MOP::Instance->meta->add_attribute(
670 Class::MOP::Attribute->new('_class_name',
672 reader => { _class_name => \&Class::MOP::Instance::_class_name },
673 #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway
674 #default => sub { $_[0]->associated_metaclass->name },
678 Class::MOP::Instance->meta->add_attribute(
679 Class::MOP::Attribute->new('attributes',
680 reader => { attributes => \&Class::MOP::Instance::attributes },
684 Class::MOP::Instance->meta->add_attribute(
685 Class::MOP::Attribute->new('slots',
686 reader => { slots => \&Class::MOP::Instance::slots },
690 Class::MOP::Instance->meta->add_attribute(
691 Class::MOP::Attribute->new('slot_hash',
692 reader => { slot_hash => \&Class::MOP::Instance::slot_hash },
697 # we need the meta instance of the meta instance to be created now, in order
698 # for the constructor to be able to use it
699 Class::MOP::Instance->meta->get_meta_instance;
701 Class::MOP::Instance->meta->add_method('new' => sub {
703 my $options = $class->BUILDARGS(@_);
705 my $self = $class->meta->new_object(%$options);
707 Scalar::Util::weaken($self->{'associated_metaclass'});
712 # pretend the add_method never happenned. it hasn't yet affected anything
713 undef Class::MOP::Instance->meta->{_package_cache_flag};
715 ## --------------------------------------------------------
716 ## Now close all the Class::MOP::* classes
719 # we don't need to inline the
720 # constructors or the accessors
721 # this only lengthens the compile
722 # time of the MOP, and gives us
723 # no actual benefits.
725 $_->meta->make_immutable(
726 inline_constructor => 0,
727 inline_accessors => 0,
733 Class::MOP::Attribute
739 Class::MOP::Method::Generated
741 Class::MOP::Method::Accessor
742 Class::MOP::Method::Constructor
743 Class::MOP::Method::Wrapped
754 Class::MOP - A Meta Object Protocol for Perl 5
758 This module is a fully functioning meta object protocol for the
759 Perl 5 object system. It makes no attempt to change the behavior or
760 characteristics of the Perl 5 object system, only to create a
761 protocol for its manipulation and introspection.
763 That said, it does attempt to create the tools for building a rich
764 set of extensions to the Perl 5 object system. Every attempt has been
765 made for these tools to keep to the spirit of the Perl 5 object
766 system that we all know and love.
768 This documentation is admittedly sparse on details, as time permits
769 I will try to improve them. For now, I suggest looking at the items
770 listed in the L<SEE ALSO> section for more information. In particular
771 the book "The Art of the Meta Object Protocol" was very influential
772 in the development of this system.
774 =head2 What is a Meta Object Protocol?
776 A meta object protocol is an API to an object system.
778 To be more specific, it is a set of abstractions of the components of
779 an object system (typically things like; classes, object, methods,
780 object attributes, etc.). These abstractions can then be used to both
781 inspect and manipulate the object system which they describe.
783 It can be said that there are two MOPs for any object system; the
784 implicit MOP, and the explicit MOP. The implicit MOP handles things
785 like method dispatch or inheritance, which happen automatically as
786 part of how the object system works. The explicit MOP typically
787 handles the introspection/reflection features of the object system.
788 All object systems have implicit MOPs, without one, they would not
789 work. Explict MOPs however as less common, and depending on the
790 language can vary from restrictive (Reflection in Java or C#) to
791 wide open (CLOS is a perfect example).
793 =head2 Yet Another Class Builder!! Why?
795 This is B<not> a class builder so much as it is a I<class builder
796 B<builder>>. My intent is that an end user does not use this module
797 directly, but instead this module is used by module authors to
798 build extensions and features onto the Perl 5 object system.
800 =head2 Who is this module for?
802 This module is specifically for anyone who has ever created or
803 wanted to create a module for the Class:: namespace. The tools which
804 this module will provide will hopefully make it easier to do more
805 complex things with Perl 5 classes by removing such barriers as
806 the need to hack the symbol tables, or understand the fine details
809 =head2 What changes do I have to make to use this module?
811 This module was designed to be as unintrusive as possible. Many of
812 its features are accessible without B<any> change to your existsing
813 code at all. It is meant to be a compliment to your existing code and
814 not an intrusion on your code base. Unlike many other B<Class::>
815 modules, this module B<does not> require you subclass it, or even that
816 you C<use> it in within your module's package.
818 The only features which requires additions to your code are the
819 attribute handling and instance construction features, and these are
820 both completely optional features. The only reason for this is because
821 Perl 5's object system does not actually have these features built
822 in. More information about this feature can be found below.
824 =head2 A Note about Performance?
826 It is a common misconception that explict MOPs are performance drains.
827 But this is not a universal truth at all, it is an side-effect of
828 specific implementations. For instance, using Java reflection is much
829 slower because the JVM cannot take advantage of any compiler
830 optimizations, and the JVM has to deal with much more runtime type
831 information as well. Reflection in C# is marginally better as it was
832 designed into the language and runtime (the CLR). In contrast, CLOS
833 (the Common Lisp Object System) was built to support an explicit MOP,
834 and so performance is tuned for it.
836 This library in particular does it's absolute best to avoid putting
837 B<any> drain at all upon your code's performance. In fact, by itself
838 it does nothing to affect your existing code. So you only pay for
839 what you actually use.
841 =head2 About Metaclass compatibility
843 This module makes sure that all metaclasses created are both upwards
844 and downwards compatible. The topic of metaclass compatibility is
845 highly esoteric and is something only encountered when doing deep and
846 involved metaclass hacking. There are two basic kinds of metaclass
847 incompatibility; upwards and downwards.
849 Upwards metaclass compatibility means that the metaclass of a
850 given class is either the same as (or a subclass of) all of the
853 Downward metaclass compatibility means that the metaclasses of a
854 given class's anscestors are all either the same as (or a subclass
857 Here is a diagram showing a set of two classes (C<A> and C<B>) and
858 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
859 metaclass compatibility both upwards and downwards.
861 +---------+ +---------+
862 | Meta::A |<----| Meta::B | <....... (instance of )
863 +---------+ +---------+ <------- (inherits from)
866 +---------+ +---------+
868 +---------+ +---------+
870 As I said this is a highly esoteric topic and one you will only run
871 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
872 are interested in why this is an issue see the paper
873 I<Uniform and safe metaclass composition> linked to in the
874 L<SEE ALSO> section of this document.
876 =head2 Using custom metaclasses
878 Always use the metaclass pragma when using a custom metaclass, this
879 will ensure the proper initialization order and not accidentely
880 create an incorrect type of metaclass for you. This is a very rare
881 problem, and one which can only occur if you are doing deep metaclass
882 programming. So in other words, don't worry about it.
886 The protocol is divided into 4 main sub-protocols:
890 =item The Class protocol
892 This provides a means of manipulating and introspecting a Perl 5
893 class. It handles all of symbol table hacking for you, and provides
894 a rich set of methods that go beyond simple package introspection.
896 See L<Class::MOP::Class> for more details.
898 =item The Attribute protocol
900 This provides a consistent represenation for an attribute of a
901 Perl 5 class. Since there are so many ways to create and handle
902 attributes in Perl 5 OO, this attempts to provide as much of a
903 unified approach as possible, while giving the freedom and
904 flexibility to subclass for specialization.
906 See L<Class::MOP::Attribute> for more details.
908 =item The Method protocol
910 This provides a means of manipulating and introspecting methods in
911 the Perl 5 object system. As with attributes, there are many ways to
912 approach this topic, so we try to keep it pretty basic, while still
913 making it possible to extend the system in many ways.
915 See L<Class::MOP::Method> for more details.
917 =item The Instance protocol
919 This provides a layer of abstraction for creating object instances.
920 Since the other layers use this protocol, it is relatively easy to
921 change the type of your instances from the default HASH ref to other
922 types of references. Several examples are provided in the F<examples/>
923 directory included in this distribution.
925 See L<Class::MOP::Instance> for more details.
935 =item I<IS_RUNNING_ON_5_10>
937 We set this constant depending on what version perl we are on, this
938 allows us to take advantage of new 5.10 features and stay backwards
943 Whether or not C<mro> provides C<get_isarev>, a much faster way to get all the
944 subclasses of a certain class.
948 =head2 Utility functions
952 =item B<load_class ($class_name)>
954 This will load a given C<$class_name> and if it does not have an
955 already initialized metaclass, then it will intialize one for it.
956 This function can be used in place of tricks like
957 C<eval "use $module"> or using C<require>.
959 =item B<is_class_loaded ($class_name)>
961 This will return a boolean depending on if the C<$class_name> has
964 NOTE: This does a basic check of the symbol table to try and
965 determine as best it can if the C<$class_name> is loaded, it
966 is probably correct about 99% of the time.
968 =item B<check_package_cache_flag ($pkg)>
970 This will return an integer that is managed by C<Class::MOP::Class>
971 to determine if a module's symbol table has been altered.
973 In Perl 5.10 or greater, this flag is package specific. However in
974 versions prior to 5.10, this will use the C<PL_sub_generation> variable
975 which is not package specific.
977 =item B<get_code_info ($code)>
979 This function returns two values, the name of the package the C<$code>
980 is from and the name of the C<$code> itself. This is used by several
981 elements of the MOP to detemine where a given C<$code> reference is from.
983 =item B<subname ($name, $code)>
985 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
987 If possible, we will load the L<Sub::Name> module and this will function
988 as C<Sub::Name::subname> does, otherwise it will just return the C<$code>
993 =head2 Metaclass cache functions
995 Class::MOP holds a cache of metaclasses, the following are functions
996 (B<not methods>) which can be used to access that cache. It is not
997 recommended that you mess with this, bad things could happen. But if
998 you are brave and willing to risk it, go for it.
1002 =item B<get_all_metaclasses>
1004 This will return an hash of all the metaclass instances that have
1005 been cached by B<Class::MOP::Class> keyed by the package name.
1007 =item B<get_all_metaclass_instances>
1009 This will return an array of all the metaclass instances that have
1010 been cached by B<Class::MOP::Class>.
1012 =item B<get_all_metaclass_names>
1014 This will return an array of all the metaclass names that have
1015 been cached by B<Class::MOP::Class>.
1017 =item B<get_metaclass_by_name ($name)>
1019 This will return a cached B<Class::MOP::Class> instance of nothing
1020 if no metaclass exist by that C<$name>.
1022 =item B<store_metaclass_by_name ($name, $meta)>
1024 This will store a metaclass in the cache at the supplied C<$key>.
1026 =item B<weaken_metaclass ($name)>
1028 In rare cases it is desireable to store a weakened reference in
1029 the metaclass cache. This function will weaken the reference to
1030 the metaclass stored in C<$name>.
1032 =item B<does_metaclass_exist ($name)>
1034 This will return true of there exists a metaclass stored in the
1035 C<$name> key and return false otherwise.
1037 =item B<remove_metaclass_by_name ($name)>
1039 This will remove a the metaclass stored in the C<$name> key.
1047 There are very few books out on Meta Object Protocols and Metaclasses
1048 because it is such an esoteric topic. The following books are really
1049 the only ones I have found. If you know of any more, B<I<please>>
1050 email me and let me know, I would love to hear about them.
1054 =item "The Art of the Meta Object Protocol"
1056 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
1058 =item "Putting MetaClasses to Work"
1060 =item "Smalltalk: The Language"
1068 =item Uniform and safe metaclass composition
1070 An excellent paper by the people who brought us the original Traits paper.
1071 This paper is on how Traits can be used to do safe metaclass composition,
1072 and offers an excellent introduction section which delves into the topic of
1073 metaclass compatibility.
1075 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
1077 =item Safe Metaclass Programming
1079 This paper seems to precede the above paper, and propose a mix-in based
1080 approach as opposed to the Traits based approach. Both papers have similar
1081 information on the metaclass compatibility problem space.
1083 L<http://citeseer.ist.psu.edu/37617.html>
1091 =item The Perl 6 MetaModel work in the Pugs project
1095 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
1097 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
1107 =item CPAN Module Review of Class::MOP
1109 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1113 =head1 SIMILAR MODULES
1115 As I have said above, this module is a class-builder-builder, so it is
1116 not the same thing as modules like L<Class::Accessor> and
1117 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1118 with similar goals to this module. The one I have found which is most
1119 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1120 creates are very different from this modules.
1124 All complex software has bugs lurking in it, and this module is no
1125 exception. If you find a bug please either email me, or add the bug
1128 =head1 ACKNOWLEDGEMENTS
1134 Thanks to Rob for actually getting the development of this module kick-started.
1140 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1142 B<with contributions from:>
1144 Brandon (blblack) Black
1146 Guillermo (groditi) Roditi
1150 Rob (robkinyon) Kinyon
1152 Yuval (nothingmuch) Kogman
1154 Scott (konobi) McWhirter
1156 =head1 COPYRIGHT AND LICENSE
1158 Copyright 2006-2008 by Infinity Interactive, Inc.
1160 L<http://www.iinteractive.com>
1162 This library is free software; you can redistribute it and/or modify
1163 it under the same terms as Perl itself.