10 use Scalar::Util 'weaken';
12 use Class::MOP::Class;
13 use Class::MOP::Attribute;
14 use Class::MOP::Method;
16 use Class::MOP::Immutable;
20 our $VERSION = '0.65';
21 our $AUTHORITY = 'cpan:STEVAN';
23 *IS_RUNNING_ON_5_10 = ($] < 5.009_005)
27 *HAVE_ISAREV = defined(&mro::get_isarev)
32 # we may not use this yet, but once
33 # the get_code_info XS gets merged
34 # upstream to it, we will always use
35 # it. But for now it is just kinda
38 require Sub::Identify;
40 # stash these for a sec, and see how things go
41 my $_PP_subname = sub { $_[1] };
42 my $_PP_get_code_info = \&Sub::Identify::get_code_info;
44 if ($ENV{CLASS_MOP_NO_XS}) {
46 # this is if you really want things
47 # to be slow, then you can force the
48 # no-XS rule this way, otherwise we
49 # make an effort to load as much of
52 no warnings 'prototype', 'redefine';
54 # this is either part of core or set up appropriately by MRO::Compat
55 *check_package_cache_flag = \&mro::get_pkg_gen;
57 # our own version of Sub::Name
58 *subname = $_PP_subname;
59 # and the Sub::Identify version of the get_code_info
60 *get_code_info = $_PP_get_code_info;
63 # now try our best to get as much
64 # of the XS loaded as possible
69 XSLoader::load( 'Class::MOP', $VERSION );
71 die $@ if $@ && $@ !~ /object version|loadable object/;
73 # okay, so the XS failed to load, so
74 # use the pure perl one instead.
75 *get_code_info = $_PP_get_code_info if $@;
78 # get it from MRO::Compat
79 *check_package_cache_flag = \&mro::get_pkg_gen;
81 # now try and load the Sub::Name
82 # module and use that as a means
83 # for naming our CVs, if not, we
84 # use the workaround instead.
86 if ( eval { require Sub::Name } ) {
87 *subname = \&Sub::Name::subname;
90 *subname = $_PP_subname;
96 # Metaclasses are singletons, so we cache them here.
97 # there is no need to worry about destruction though
98 # because they should die only when the program dies.
99 # After all, do package definitions even get reaped?
102 # means of accessing all the metaclasses that have
103 # been initialized thus far (for mugwumps obj browser)
104 sub get_all_metaclasses { %METAS }
105 sub get_all_metaclass_instances { values %METAS }
106 sub get_all_metaclass_names { keys %METAS }
107 sub get_metaclass_by_name { $METAS{$_[0]} }
108 sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
109 sub weaken_metaclass { weaken($METAS{$_[0]}) }
110 sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} }
111 sub remove_metaclass_by_name { $METAS{$_[0]} = undef }
114 # We only cache metaclasses, meaning instances of
115 # Class::MOP::Class. We do not cache instance of
116 # Class::MOP::Package or Class::MOP::Module. Mostly
117 # because I don't yet see a good reason to do so.
123 if (ref($class) || !defined($class) || !length($class)) {
124 my $display = defined($class) ? $class : 'undef';
125 confess "Invalid class name ($display)";
128 # if the class is not already loaded in the symbol table..
129 unless (is_class_loaded($class)) {
131 my $file = $class . '.pm';
133 my $e = do { local $@; eval { require($file) }; $@ };
134 confess "Could not load class ($class) because : $e" if $e;
137 # initialize a metaclass if necessary
138 unless (does_metaclass_exist($class)) {
139 my $e = do { local $@; eval { Class::MOP::Class->initialize($class) }; $@ };
140 confess "Could not initialize class ($class) because : $e" if $e;
143 return get_metaclass_by_name($class) if defined wantarray;
146 sub is_class_loaded {
149 return 0 if ref($class) || !defined($class) || !length($class);
151 # walk the symbol table tree to avoid autovififying
152 # \*{${main::}{"Foo::"}} == \*main::Foo::
155 foreach my $part (split('::', $class)) {
156 return 0 unless exists ${$$pack}{"${part}::"};
157 $pack = \*{${$$pack}{"${part}::"}};
160 # check for $VERSION or @ISA
161 return 1 if exists ${$$pack}{VERSION}
162 && defined *{${$$pack}{VERSION}}{SCALAR};
163 return 1 if exists ${$$pack}{ISA}
164 && defined *{${$$pack}{ISA}}{ARRAY};
166 # check for any method
167 foreach ( keys %{$$pack} ) {
168 next if substr($_, -2, 2) eq '::';
170 my $glob = ${$$pack}{$_} || next;
173 if ( IS_RUNNING_ON_5_10 ) {
174 return 1 if ref $glob eq 'SCALAR';
177 return 1 if defined *{$glob}{CODE};
185 ## ----------------------------------------------------------------------------
186 ## Setting up our environment ...
187 ## ----------------------------------------------------------------------------
188 ## Class::MOP needs to have a few things in the global perl environment so
189 ## that it can operate effectively. Those things are done here.
190 ## ----------------------------------------------------------------------------
192 # ... nothing yet actually ;)
194 ## ----------------------------------------------------------------------------
196 ## ----------------------------------------------------------------------------
197 ## The code below here is to bootstrap our MOP with itself. This is also
198 ## sometimes called "tying the knot". By doing this, we make it much easier
199 ## to extend the MOP through subclassing and such since now you can use the
200 ## MOP itself to extend itself.
202 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
203 ## ----------------------------------------------------------------------------
205 # We need to add in the meta-attributes here so that
206 # any subclass of Class::MOP::* will be able to
207 # inherit them using &construct_instance
209 ## --------------------------------------------------------
210 ## Class::MOP::Package
212 Class::MOP::Package->meta->add_attribute(
213 Class::MOP::Attribute->new('package' => (
215 # NOTE: we need to do this in order
216 # for the instance meta-object to
217 # not fall into meta-circular death
219 # we just alias the original method
220 # rather than re-produce it here
221 'name' => \&Class::MOP::Package::name
226 Class::MOP::Package->meta->add_attribute(
227 Class::MOP::Attribute->new('namespace' => (
230 # we just alias the original method
231 # rather than re-produce it here
232 'namespace' => \&Class::MOP::Package::namespace
235 default => sub { \undef }
240 # use the metaclass to construct the meta-package
241 # which is a superclass of the metaclass itself :P
242 Class::MOP::Package->meta->add_method('initialize' => sub {
244 my $package_name = shift;
245 $class->meta->new_object('package' => $package_name, @_);
248 ## --------------------------------------------------------
249 ## Class::MOP::Module
252 # yeah this is kind of stretching things a bit,
253 # but truthfully the version should be an attribute
254 # of the Module, the weirdness comes from having to
255 # stick to Perl 5 convention and store it in the
256 # $VERSION package variable. Basically if you just
257 # squint at it, it will look how you want it to look.
258 # Either as a package variable, or as a attribute of
259 # the metaclass, isn't abstraction great :)
261 Class::MOP::Module->meta->add_attribute(
262 Class::MOP::Attribute->new('version' => (
265 # we just alias the original method
266 # rather than re-produce it here
267 'version' => \&Class::MOP::Module::version
270 default => sub { \undef }
275 # By following the same conventions as version here,
276 # we are opening up the possibility that people can
277 # use the $AUTHORITY in non-Class::MOP modules as
280 Class::MOP::Module->meta->add_attribute(
281 Class::MOP::Attribute->new('authority' => (
284 # we just alias the original method
285 # rather than re-produce it here
286 'authority' => \&Class::MOP::Module::authority
289 default => sub { \undef }
293 ## --------------------------------------------------------
296 Class::MOP::Class->meta->add_attribute(
297 Class::MOP::Attribute->new('attributes' => (
299 # NOTE: we need to do this in order
300 # for the instance meta-object to
301 # not fall into meta-circular death
303 # we just alias the original method
304 # rather than re-produce it here
305 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
307 default => sub { {} }
311 Class::MOP::Class->meta->add_attribute(
312 Class::MOP::Attribute->new('methods' => (
315 # we just alias the original method
316 # rather than re-produce it here
317 'get_method_map' => \&Class::MOP::Class::get_method_map
319 default => sub { {} }
323 Class::MOP::Class->meta->add_attribute(
324 Class::MOP::Attribute->new('superclasses' => (
327 # we just alias the original method
328 # rather than re-produce it here
329 'superclasses' => \&Class::MOP::Class::superclasses
332 default => sub { \undef }
336 Class::MOP::Class->meta->add_attribute(
337 Class::MOP::Attribute->new('attribute_metaclass' => (
340 # we just alias the original method
341 # rather than re-produce it here
342 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
344 default => 'Class::MOP::Attribute',
348 Class::MOP::Class->meta->add_attribute(
349 Class::MOP::Attribute->new('method_metaclass' => (
352 # we just alias the original method
353 # rather than re-produce it here
354 'method_metaclass' => \&Class::MOP::Class::method_metaclass
356 default => 'Class::MOP::Method',
360 Class::MOP::Class->meta->add_attribute(
361 Class::MOP::Attribute->new('instance_metaclass' => (
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 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
371 default => 'Class::MOP::Instance',
376 # we don't actually need to tie the knot with
377 # Class::MOP::Class here, it is actually handled
378 # within Class::MOP::Class itself in the
379 # construct_class_instance method.
381 ## --------------------------------------------------------
382 ## Class::MOP::Attribute
384 Class::MOP::Attribute->meta->add_attribute(
385 Class::MOP::Attribute->new('name' => (
387 # NOTE: we need to do this in order
388 # for the instance meta-object to
389 # not fall into meta-circular death
391 # we just alias the original method
392 # rather than re-produce it here
393 'name' => \&Class::MOP::Attribute::name
398 Class::MOP::Attribute->meta->add_attribute(
399 Class::MOP::Attribute->new('associated_class' => (
401 # NOTE: we need to do this in order
402 # for the instance meta-object to
403 # not fall into meta-circular death
405 # we just alias the original method
406 # rather than re-produce it here
407 'associated_class' => \&Class::MOP::Attribute::associated_class
412 Class::MOP::Attribute->meta->add_attribute(
413 Class::MOP::Attribute->new('accessor' => (
414 reader => { 'accessor' => \&Class::MOP::Attribute::accessor },
415 predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
419 Class::MOP::Attribute->meta->add_attribute(
420 Class::MOP::Attribute->new('reader' => (
421 reader => { 'reader' => \&Class::MOP::Attribute::reader },
422 predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
426 Class::MOP::Attribute->meta->add_attribute(
427 Class::MOP::Attribute->new('initializer' => (
428 reader => { 'initializer' => \&Class::MOP::Attribute::initializer },
429 predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
433 Class::MOP::Attribute->meta->add_attribute(
434 Class::MOP::Attribute->new('writer' => (
435 reader => { 'writer' => \&Class::MOP::Attribute::writer },
436 predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
440 Class::MOP::Attribute->meta->add_attribute(
441 Class::MOP::Attribute->new('predicate' => (
442 reader => { 'predicate' => \&Class::MOP::Attribute::predicate },
443 predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate },
447 Class::MOP::Attribute->meta->add_attribute(
448 Class::MOP::Attribute->new('clearer' => (
449 reader => { 'clearer' => \&Class::MOP::Attribute::clearer },
450 predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer },
454 Class::MOP::Attribute->meta->add_attribute(
455 Class::MOP::Attribute->new('builder' => (
456 reader => { 'builder' => \&Class::MOP::Attribute::builder },
457 predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder },
461 Class::MOP::Attribute->meta->add_attribute(
462 Class::MOP::Attribute->new('init_arg' => (
463 reader => { 'init_arg' => \&Class::MOP::Attribute::init_arg },
464 predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg },
468 Class::MOP::Attribute->meta->add_attribute(
469 Class::MOP::Attribute->new('default' => (
470 # default has a custom 'reader' method ...
471 predicate => { 'has_default' => \&Class::MOP::Attribute::has_default },
475 Class::MOP::Attribute->meta->add_attribute(
476 Class::MOP::Attribute->new('associated_methods' => (
477 reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods },
478 default => sub { [] }
482 # NOTE: (meta-circularity)
483 # This should be one of the last things done
484 # it will "tie the knot" with Class::MOP::Attribute
485 # so that it uses the attributes meta-objects
486 # to construct itself.
487 Class::MOP::Attribute->meta->add_method('new' => sub {
488 my ( $class, @args ) = @_;
490 unshift @args, "name" if @args % 2 == 1;
493 my $name = $options{name};
495 (defined $name && $name)
496 || confess "You must provide a name for the attribute";
497 $options{init_arg} = $name
498 if not exists $options{init_arg};
500 if(exists $options{builder}){
501 confess("builder must be a defined scalar value which is a method name")
502 if ref $options{builder} || !(defined $options{builder});
503 confess("Setting both default and builder is not allowed.")
504 if exists $options{default};
506 (Class::MOP::Attribute::is_default_a_coderef(\%options))
507 || confess("References are not allowed as default values, you must ".
508 "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])")
509 if exists $options{default} && ref $options{default};
512 # return the new object
513 $class->meta->new_object(%options);
516 Class::MOP::Attribute->meta->add_method('clone' => sub {
518 $self->meta->clone_object($self, @_);
521 ## --------------------------------------------------------
522 ## Class::MOP::Method
523 Class::MOP::Method->meta->add_attribute(
524 Class::MOP::Attribute->new('body' => (
525 reader => { 'body' => \&Class::MOP::Method::body },
529 Class::MOP::Method->meta->add_attribute(
530 Class::MOP::Attribute->new('associated_metaclass' => (
531 reader => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass },
535 Class::MOP::Method->meta->add_attribute(
536 Class::MOP::Attribute->new('package_name' => (
537 reader => { 'package_name' => \&Class::MOP::Method::package_name },
541 Class::MOP::Method->meta->add_attribute(
542 Class::MOP::Attribute->new('name' => (
543 reader => { 'name' => \&Class::MOP::Method::name },
547 # FIMXE prime candidate for immutablization
548 Class::MOP::Method->meta->add_method('wrap' => sub {
549 my ( $class, @args ) = @_;
551 unshift @args, 'body' if @args % 2 == 1;
554 my $code = $options{body};
556 ('CODE' eq ref($code))
557 || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
559 ($options{package_name} && $options{name})
560 || confess "You must supply the package_name and name parameters";
562 # return the new object
563 $class->meta->new_object(%options);
566 Class::MOP::Method->meta->add_method('clone' => sub {
568 $self->meta->clone_object($self, @_);
571 ## --------------------------------------------------------
572 ## Class::MOP::Method::Wrapped
575 # the way this item is initialized, this
576 # really does not follow the standard
577 # practices of attributes, but we put
578 # it here for completeness
579 Class::MOP::Method::Wrapped->meta->add_attribute(
580 Class::MOP::Attribute->new('modifier_table')
583 ## --------------------------------------------------------
584 ## Class::MOP::Method::Generated
586 Class::MOP::Method::Generated->meta->add_attribute(
587 Class::MOP::Attribute->new('is_inline' => (
588 reader => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline },
593 Class::MOP::Method::Generated->meta->add_method('new' => sub {
594 my ($class, %options) = @_;
595 ($options{package_name} && $options{name})
596 || confess "You must supply the package_name and name parameters";
597 my $self = $class->meta->new_object(%options);
598 $self->initialize_body;
602 ## --------------------------------------------------------
603 ## Class::MOP::Method::Accessor
605 Class::MOP::Method::Accessor->meta->add_attribute(
606 Class::MOP::Attribute->new('attribute' => (
608 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
613 Class::MOP::Method::Accessor->meta->add_attribute(
614 Class::MOP::Attribute->new('accessor_type' => (
615 reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
619 Class::MOP::Method::Accessor->meta->add_method('new' => sub {
623 (exists $options{attribute})
624 || confess "You must supply an attribute to construct with";
626 (exists $options{accessor_type})
627 || confess "You must supply an accessor_type to construct with";
629 (Scalar::Util::blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
630 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
632 ($options{package_name} && $options{name})
633 || confess "You must supply the package_name and name parameters";
635 # return the new object
636 my $self = $class->meta->new_object(%options);
638 # we don't want this creating
639 # a cycle in the code, if not
641 Scalar::Util::weaken($self->{'attribute'});
643 $self->initialize_body;
649 ## --------------------------------------------------------
650 ## Class::MOP::Method::Constructor
652 Class::MOP::Method::Constructor->meta->add_attribute(
653 Class::MOP::Attribute->new('options' => (
655 'options' => \&Class::MOP::Method::Constructor::options
657 default => sub { +{} }
661 Class::MOP::Method::Constructor->meta->add_attribute(
662 Class::MOP::Attribute->new('associated_metaclass' => (
663 init_arg => "metaclass", # FIXME alias and rename
665 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
670 Class::MOP::Method::Constructor->meta->add_method('new' => sub {
674 (Scalar::Util::blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
675 || confess "You must pass a metaclass instance if you want to inline"
676 if $options{is_inline};
678 ($options{package_name} && $options{name})
679 || confess "You must supply the package_name and name parameters";
681 # return the new object
682 my $self = $class->meta->new_object(%options);
684 # we don't want this creating
685 # a cycle in the code, if not
687 Scalar::Util::weaken($self->{'associated_metaclass'});
689 $self->initialize_body;
694 ## --------------------------------------------------------
695 ## Class::MOP::Instance
698 # these don't yet do much of anything, but are just
699 # included for completeness
701 Class::MOP::Instance->meta->add_attribute(
702 Class::MOP::Attribute->new('associated_metaclass')
705 Class::MOP::Instance->meta->add_attribute(
706 Class::MOP::Attribute->new('attributes')
709 Class::MOP::Instance->meta->add_attribute(
710 Class::MOP::Attribute->new('slots')
713 Class::MOP::Instance->meta->add_attribute(
714 Class::MOP::Attribute->new('slot_hash')
718 # we need the meta instance of the meta instance to be created now, in order
719 # for the constructor to be able to use it
720 Class::MOP::Instance->meta->get_meta_instance;
722 Class::MOP::Instance->meta->add_method('new' => sub {
724 my $options = $class->BUILDARGS(@_);
726 my $self = $class->meta->new_object(%$options);
728 Scalar::Util::weaken($self->{'associated_metaclass'});
733 # pretend the add_method never happenned. it hasn't yet affected anything
734 undef Class::MOP::Instance->meta->{_package_cache_flag};
736 ## --------------------------------------------------------
737 ## Now close all the Class::MOP::* classes
740 # we don't need to inline the
741 # constructors or the accessors
742 # this only lengthens the compile
743 # time of the MOP, and gives us
744 # no actual benefits.
746 $_->meta->make_immutable(
747 inline_constructor => 0,
748 inline_accessors => 0,
754 Class::MOP::Attribute
760 Class::MOP::Method::Generated
762 Class::MOP::Method::Accessor
763 Class::MOP::Method::Constructor
764 Class::MOP::Method::Wrapped
775 Class::MOP - A Meta Object Protocol for Perl 5
779 This module is a fully functioning meta object protocol for the
780 Perl 5 object system. It makes no attempt to change the behavior or
781 characteristics of the Perl 5 object system, only to create a
782 protocol for its manipulation and introspection.
784 That said, it does attempt to create the tools for building a rich
785 set of extensions to the Perl 5 object system. Every attempt has been
786 made for these tools to keep to the spirit of the Perl 5 object
787 system that we all know and love.
789 This documentation is admittedly sparse on details, as time permits
790 I will try to improve them. For now, I suggest looking at the items
791 listed in the L<SEE ALSO> section for more information. In particular
792 the book "The Art of the Meta Object Protocol" was very influential
793 in the development of this system.
795 =head2 What is a Meta Object Protocol?
797 A meta object protocol is an API to an object system.
799 To be more specific, it is a set of abstractions of the components of
800 an object system (typically things like; classes, object, methods,
801 object attributes, etc.). These abstractions can then be used to both
802 inspect and manipulate the object system which they describe.
804 It can be said that there are two MOPs for any object system; the
805 implicit MOP, and the explicit MOP. The implicit MOP handles things
806 like method dispatch or inheritance, which happen automatically as
807 part of how the object system works. The explicit MOP typically
808 handles the introspection/reflection features of the object system.
809 All object systems have implicit MOPs, without one, they would not
810 work. Explict MOPs however as less common, and depending on the
811 language can vary from restrictive (Reflection in Java or C#) to
812 wide open (CLOS is a perfect example).
814 =head2 Yet Another Class Builder!! Why?
816 This is B<not> a class builder so much as it is a I<class builder
817 B<builder>>. My intent is that an end user does not use this module
818 directly, but instead this module is used by module authors to
819 build extensions and features onto the Perl 5 object system.
821 =head2 Who is this module for?
823 This module is specifically for anyone who has ever created or
824 wanted to create a module for the Class:: namespace. The tools which
825 this module will provide will hopefully make it easier to do more
826 complex things with Perl 5 classes by removing such barriers as
827 the need to hack the symbol tables, or understand the fine details
830 =head2 What changes do I have to make to use this module?
832 This module was designed to be as unintrusive as possible. Many of
833 its features are accessible without B<any> change to your existsing
834 code at all. It is meant to be a compliment to your existing code and
835 not an intrusion on your code base. Unlike many other B<Class::>
836 modules, this module B<does not> require you subclass it, or even that
837 you C<use> it in within your module's package.
839 The only features which requires additions to your code are the
840 attribute handling and instance construction features, and these are
841 both completely optional features. The only reason for this is because
842 Perl 5's object system does not actually have these features built
843 in. More information about this feature can be found below.
845 =head2 A Note about Performance?
847 It is a common misconception that explict MOPs are performance drains.
848 But this is not a universal truth at all, it is an side-effect of
849 specific implementations. For instance, using Java reflection is much
850 slower because the JVM cannot take advantage of any compiler
851 optimizations, and the JVM has to deal with much more runtime type
852 information as well. Reflection in C# is marginally better as it was
853 designed into the language and runtime (the CLR). In contrast, CLOS
854 (the Common Lisp Object System) was built to support an explicit MOP,
855 and so performance is tuned for it.
857 This library in particular does it's absolute best to avoid putting
858 B<any> drain at all upon your code's performance. In fact, by itself
859 it does nothing to affect your existing code. So you only pay for
860 what you actually use.
862 =head2 About Metaclass compatibility
864 This module makes sure that all metaclasses created are both upwards
865 and downwards compatible. The topic of metaclass compatibility is
866 highly esoteric and is something only encountered when doing deep and
867 involved metaclass hacking. There are two basic kinds of metaclass
868 incompatibility; upwards and downwards.
870 Upwards metaclass compatibility means that the metaclass of a
871 given class is either the same as (or a subclass of) all of the
874 Downward metaclass compatibility means that the metaclasses of a
875 given class's anscestors are all either the same as (or a subclass
878 Here is a diagram showing a set of two classes (C<A> and C<B>) and
879 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
880 metaclass compatibility both upwards and downwards.
882 +---------+ +---------+
883 | Meta::A |<----| Meta::B | <....... (instance of )
884 +---------+ +---------+ <------- (inherits from)
887 +---------+ +---------+
889 +---------+ +---------+
891 As I said this is a highly esoteric topic and one you will only run
892 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
893 are interested in why this is an issue see the paper
894 I<Uniform and safe metaclass composition> linked to in the
895 L<SEE ALSO> section of this document.
897 =head2 Using custom metaclasses
899 Always use the metaclass pragma when using a custom metaclass, this
900 will ensure the proper initialization order and not accidentely
901 create an incorrect type of metaclass for you. This is a very rare
902 problem, and one which can only occur if you are doing deep metaclass
903 programming. So in other words, don't worry about it.
907 The protocol is divided into 4 main sub-protocols:
911 =item The Class protocol
913 This provides a means of manipulating and introspecting a Perl 5
914 class. It handles all of symbol table hacking for you, and provides
915 a rich set of methods that go beyond simple package introspection.
917 See L<Class::MOP::Class> for more details.
919 =item The Attribute protocol
921 This provides a consistent represenation for an attribute of a
922 Perl 5 class. Since there are so many ways to create and handle
923 attributes in Perl 5 OO, this attempts to provide as much of a
924 unified approach as possible, while giving the freedom and
925 flexibility to subclass for specialization.
927 See L<Class::MOP::Attribute> for more details.
929 =item The Method protocol
931 This provides a means of manipulating and introspecting methods in
932 the Perl 5 object system. As with attributes, there are many ways to
933 approach this topic, so we try to keep it pretty basic, while still
934 making it possible to extend the system in many ways.
936 See L<Class::MOP::Method> for more details.
938 =item The Instance protocol
940 This provides a layer of abstraction for creating object instances.
941 Since the other layers use this protocol, it is relatively easy to
942 change the type of your instances from the default HASH ref to other
943 types of references. Several examples are provided in the F<examples/>
944 directory included in this distribution.
946 See L<Class::MOP::Instance> for more details.
956 =item I<IS_RUNNING_ON_5_10>
958 We set this constant depending on what version perl we are on, this
959 allows us to take advantage of new 5.10 features and stay backwards
964 Whether or not C<mro> provides C<get_isarev>, a much faster way to get all the
965 subclasses of a certain class.
969 =head2 Utility functions
973 =item B<load_class ($class_name)>
975 This will load a given C<$class_name> and if it does not have an
976 already initialized metaclass, then it will intialize one for it.
977 This function can be used in place of tricks like
978 C<eval "use $module"> or using C<require>.
980 =item B<is_class_loaded ($class_name)>
982 This will return a boolean depending on if the C<$class_name> has
985 NOTE: This does a basic check of the symbol table to try and
986 determine as best it can if the C<$class_name> is loaded, it
987 is probably correct about 99% of the time.
989 =item B<check_package_cache_flag ($pkg)>
991 This will return an integer that is managed by C<Class::MOP::Class>
992 to determine if a module's symbol table has been altered.
994 In Perl 5.10 or greater, this flag is package specific. However in
995 versions prior to 5.10, this will use the C<PL_sub_generation> variable
996 which is not package specific.
998 =item B<get_code_info ($code)>
1000 This function returns two values, the name of the package the C<$code>
1001 is from and the name of the C<$code> itself. This is used by several
1002 elements of the MOP to detemine where a given C<$code> reference is from.
1004 =item B<subname ($name, $code)>
1006 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
1008 If possible, we will load the L<Sub::Name> module and this will function
1009 as C<Sub::Name::subname> does, otherwise it will just return the C<$code>
1014 =head2 Metaclass cache functions
1016 Class::MOP holds a cache of metaclasses, the following are functions
1017 (B<not methods>) which can be used to access that cache. It is not
1018 recommended that you mess with this, bad things could happen. But if
1019 you are brave and willing to risk it, go for it.
1023 =item B<get_all_metaclasses>
1025 This will return an hash of all the metaclass instances that have
1026 been cached by B<Class::MOP::Class> keyed by the package name.
1028 =item B<get_all_metaclass_instances>
1030 This will return an array of all the metaclass instances that have
1031 been cached by B<Class::MOP::Class>.
1033 =item B<get_all_metaclass_names>
1035 This will return an array of all the metaclass names that have
1036 been cached by B<Class::MOP::Class>.
1038 =item B<get_metaclass_by_name ($name)>
1040 This will return a cached B<Class::MOP::Class> instance of nothing
1041 if no metaclass exist by that C<$name>.
1043 =item B<store_metaclass_by_name ($name, $meta)>
1045 This will store a metaclass in the cache at the supplied C<$key>.
1047 =item B<weaken_metaclass ($name)>
1049 In rare cases it is desireable to store a weakened reference in
1050 the metaclass cache. This function will weaken the reference to
1051 the metaclass stored in C<$name>.
1053 =item B<does_metaclass_exist ($name)>
1055 This will return true of there exists a metaclass stored in the
1056 C<$name> key and return false otherwise.
1058 =item B<remove_metaclass_by_name ($name)>
1060 This will remove a the metaclass stored in the C<$name> key.
1068 There are very few books out on Meta Object Protocols and Metaclasses
1069 because it is such an esoteric topic. The following books are really
1070 the only ones I have found. If you know of any more, B<I<please>>
1071 email me and let me know, I would love to hear about them.
1075 =item "The Art of the Meta Object Protocol"
1077 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
1079 =item "Putting MetaClasses to Work"
1081 =item "Smalltalk: The Language"
1089 =item Uniform and safe metaclass composition
1091 An excellent paper by the people who brought us the original Traits paper.
1092 This paper is on how Traits can be used to do safe metaclass composition,
1093 and offers an excellent introduction section which delves into the topic of
1094 metaclass compatibility.
1096 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
1098 =item Safe Metaclass Programming
1100 This paper seems to precede the above paper, and propose a mix-in based
1101 approach as opposed to the Traits based approach. Both papers have similar
1102 information on the metaclass compatibility problem space.
1104 L<http://citeseer.ist.psu.edu/37617.html>
1112 =item The Perl 6 MetaModel work in the Pugs project
1116 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
1118 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
1128 =item CPAN Module Review of Class::MOP
1130 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1134 =head1 SIMILAR MODULES
1136 As I have said above, this module is a class-builder-builder, so it is
1137 not the same thing as modules like L<Class::Accessor> and
1138 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1139 with similar goals to this module. The one I have found which is most
1140 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1141 creates are very different from this modules.
1145 All complex software has bugs lurking in it, and this module is no
1146 exception. If you find a bug please either email me, or add the bug
1149 =head1 ACKNOWLEDGEMENTS
1155 Thanks to Rob for actually getting the development of this module kick-started.
1161 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1163 B<with contributions from:>
1165 Brandon (blblack) Black
1167 Guillermo (groditi) Roditi
1171 Rob (robkinyon) Kinyon
1173 Yuval (nothingmuch) Kogman
1175 Scott (konobi) McWhirter
1177 =head1 COPYRIGHT AND LICENSE
1179 Copyright 2006-2008 by Infinity Interactive, Inc.
1181 L<http://www.iinteractive.com>
1183 This library is free software; you can redistribute it and/or modify
1184 it under the same terms as Perl itself.