When checking for a valid class name, actually use the right regex
[gitmo/Class-MOP.git] / lib / Class / MOP.pm
1
2 package Class::MOP;
3
4 use strict;
5 use warnings;
6
7 use 5.008;
8
9 use MRO::Compat;
10
11 use Carp          'confess';
12 use Scalar::Util  'weaken';
13
14 use Sub::Identify 'get_code_info';
15
16 BEGIN {
17     local $@;
18     eval {
19         require Sub::Name;
20         Sub::Name->import(qw(subname));
21         1
22     } or eval 'sub subname { $_[1] }';
23
24     # this is either part of core or set up appropriately by MRO::Compat
25     *check_package_cache_flag = \&mro::get_pkg_gen;
26
27     eval {
28         require Devel::GlobalDestruction;
29         Devel::GlobalDestruction->import("in_global_destruction");
30         1;
31     } or *in_global_destruction = sub () { !1 };
32 }
33
34
35 use Class::MOP::Class;
36 use Class::MOP::Attribute;
37 use Class::MOP::Method;
38
39 use Class::MOP::Immutable;
40
41 BEGIN {
42     *IS_RUNNING_ON_5_10 = ($] < 5.009_005) 
43         ? sub () { 0 }
44         : sub () { 1 };    
45
46     *HAVE_ISAREV = defined(&mro::get_isarev)
47         ? sub () { 1 }
48         : sub () { 1 };
49 }
50
51 our $VERSION   = '0.64_06';
52 our $XS_VERSION = $VERSION;
53 $VERSION = eval $VERSION;
54 our $AUTHORITY = 'cpan:STEVAN';    
55     
56 # after that everything is loaded, if we're allowed try to load faster XS
57 # versions of various things
58 unless ($ENV{CLASS_MOP_NO_XS}) {
59     my $e = do {
60         local $@;
61         eval {
62             require XSLoader;
63             __PACKAGE__->XSLoader::load($XS_VERSION);
64         };
65         $@;
66     };
67
68     die $e if $e && $e !~ /object version|loadable object/;
69 }
70
71 {
72     # Metaclasses are singletons, so we cache them here.
73     # there is no need to worry about destruction though
74     # because they should die only when the program dies.
75     # After all, do package definitions even get reaped?
76     my %METAS;
77
78     # means of accessing all the metaclasses that have
79     # been initialized thus far (for mugwumps obj browser)
80     sub get_all_metaclasses         {        %METAS         }
81     sub get_all_metaclass_instances { values %METAS         }
82     sub get_all_metaclass_names     { keys   %METAS         }
83     sub get_metaclass_by_name       { $METAS{$_[0]}         }
84     sub store_metaclass_by_name     { $METAS{$_[0]} = $_[1] }
85     sub weaken_metaclass            { weaken($METAS{$_[0]}) }
86     sub does_metaclass_exist        { exists $METAS{$_[0]} && defined $METAS{$_[0]} }
87     sub remove_metaclass_by_name    { $METAS{$_[0]} = undef }
88
89     # NOTE:
90     # We only cache metaclasses, meaning instances of
91     # Class::MOP::Class. We do not cache instance of
92     # Class::MOP::Package or Class::MOP::Module. Mostly
93     # because I don't yet see a good reason to do so.
94 }
95
96 sub load_class {
97     my $class = shift;
98
99     unless ( _is_valid_class_name($class) ) {
100         my $display = defined($class) ? $class : 'undef';
101         confess "Invalid class name ($display)";
102     }
103
104     # if the class is not already loaded in the symbol table..
105     unless (is_class_loaded($class)) {
106         # require it
107         my $e = do { local $@; eval "require $class"; $@ };
108         confess "Could not load class ($class) because : $e" if $e;
109     }
110
111     # initialize a metaclass if necessary
112     unless (does_metaclass_exist($class)) {
113         my $e = do { local $@; eval { Class::MOP::Class->initialize($class) }; $@ };
114         confess "Could not initialize class ($class) because : $e" if $e;
115     }
116
117     return get_metaclass_by_name($class) if defined wantarray;
118 }
119
120 sub _is_valid_class_name {
121     my $class = shift;
122
123     return 0 if ref($class);
124     return 0 unless defined($class);
125     return 0 unless length($class);
126
127     return 1 if $class =~ /^\w+(?:::\w+)*$/;
128
129     return 0;
130 }
131
132 sub is_class_loaded {
133     my $class = shift;
134
135     return 0 if ref($class) || !defined($class) || !length($class);
136
137     # walk the symbol table tree to avoid autovififying
138     # \*{${main::}{"Foo::"}} == \*main::Foo::
139
140     my $pack = \*::;
141     foreach my $part (split('::', $class)) {
142         return 0 unless exists ${$$pack}{"${part}::"};
143         $pack = \*{${$$pack}{"${part}::"}};
144     }
145
146     # check for $VERSION or @ISA
147     return 1 if exists ${$$pack}{VERSION}
148              && defined *{${$$pack}{VERSION}}{SCALAR};
149     return 1 if exists ${$$pack}{ISA}
150              && defined *{${$$pack}{ISA}}{ARRAY};
151
152     # check for any method
153     foreach ( keys %{$$pack} ) {
154         next if substr($_, -2, 2) eq '::';
155
156         my $glob = ${$$pack}{$_} || next;
157
158         # constant subs
159         if ( IS_RUNNING_ON_5_10 ) {
160             return 1 if ref $glob eq 'SCALAR';
161         }
162
163         return 1 if defined *{$glob}{CODE};
164     }
165
166     # fail
167     return 0;
168 }
169
170
171 ## ----------------------------------------------------------------------------
172 ## Setting up our environment ...
173 ## ----------------------------------------------------------------------------
174 ## Class::MOP needs to have a few things in the global perl environment so
175 ## that it can operate effectively. Those things are done here.
176 ## ----------------------------------------------------------------------------
177
178 # ... nothing yet actually ;)
179
180 ## ----------------------------------------------------------------------------
181 ## Bootstrapping
182 ## ----------------------------------------------------------------------------
183 ## The code below here is to bootstrap our MOP with itself. This is also
184 ## sometimes called "tying the knot". By doing this, we make it much easier
185 ## to extend the MOP through subclassing and such since now you can use the
186 ## MOP itself to extend itself.
187 ##
188 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
189 ## ----------------------------------------------------------------------------
190
191 # We need to add in the meta-attributes here so that
192 # any subclass of Class::MOP::* will be able to
193 # inherit them using &construct_instance
194
195 ## --------------------------------------------------------
196 ## Class::MOP::Package
197
198 Class::MOP::Package->meta->add_attribute(
199     Class::MOP::Attribute->new('package' => (
200         reader   => {
201             # NOTE: we need to do this in order
202             # for the instance meta-object to
203             # not fall into meta-circular death
204             #
205             # we just alias the original method
206             # rather than re-produce it here
207             'name' => \&Class::MOP::Package::name
208         },
209     ))
210 );
211
212 Class::MOP::Package->meta->add_attribute(
213     Class::MOP::Attribute->new('namespace' => (
214         reader => {
215             # NOTE:
216             # we just alias the original method
217             # rather than re-produce it here
218             'namespace' => \&Class::MOP::Package::namespace
219         },
220         init_arg => undef,
221         default  => sub { \undef }
222     ))
223 );
224
225 ## --------------------------------------------------------
226 ## Class::MOP::Module
227
228 # NOTE:
229 # yeah this is kind of stretching things a bit,
230 # but truthfully the version should be an attribute
231 # of the Module, the weirdness comes from having to
232 # stick to Perl 5 convention and store it in the
233 # $VERSION package variable. Basically if you just
234 # squint at it, it will look how you want it to look.
235 # Either as a package variable, or as a attribute of
236 # the metaclass, isn't abstraction great :)
237
238 Class::MOP::Module->meta->add_attribute(
239     Class::MOP::Attribute->new('version' => (
240         reader => {
241             # NOTE:
242             # we just alias the original method
243             # rather than re-produce it here
244             'version' => \&Class::MOP::Module::version
245         },
246         init_arg => undef,
247         default  => sub { \undef }
248     ))
249 );
250
251 # NOTE:
252 # By following the same conventions as version here,
253 # we are opening up the possibility that people can
254 # use the $AUTHORITY in non-Class::MOP modules as
255 # well.
256
257 Class::MOP::Module->meta->add_attribute(
258     Class::MOP::Attribute->new('authority' => (
259         reader => {
260             # NOTE:
261             # we just alias the original method
262             # rather than re-produce it here
263             'authority' => \&Class::MOP::Module::authority
264         },
265         init_arg => undef,
266         default  => sub { \undef }
267     ))
268 );
269
270 ## --------------------------------------------------------
271 ## Class::MOP::Class
272
273 Class::MOP::Class->meta->add_attribute(
274     Class::MOP::Attribute->new('attributes' => (
275         reader   => {
276             # NOTE: we need to do this in order
277             # for the instance meta-object to
278             # not fall into meta-circular death
279             #
280             # we just alias the original method
281             # rather than re-produce it here
282             'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
283         },
284         default  => sub { {} }
285     ))
286 );
287
288 Class::MOP::Class->meta->add_attribute(
289     Class::MOP::Attribute->new('methods' => (
290         reader   => {
291             # NOTE:
292             # we just alias the original method
293             # rather than re-produce it here
294             'get_method_map' => \&Class::MOP::Class::get_method_map
295         },
296         default => sub { {} }
297     ))
298 );
299
300 Class::MOP::Class->meta->add_attribute(
301     Class::MOP::Attribute->new('superclasses' => (
302         accessor => {
303             # NOTE:
304             # we just alias the original method
305             # rather than re-produce it here
306             'superclasses' => \&Class::MOP::Class::superclasses
307         },
308         init_arg => undef,
309         default  => sub { \undef }
310     ))
311 );
312
313 Class::MOP::Class->meta->add_attribute(
314     Class::MOP::Attribute->new('attribute_metaclass' => (
315         reader   => {
316             # NOTE:
317             # we just alias the original method
318             # rather than re-produce it here
319             'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
320         },
321         default  => 'Class::MOP::Attribute',
322     ))
323 );
324
325 Class::MOP::Class->meta->add_attribute(
326     Class::MOP::Attribute->new('method_metaclass' => (
327         reader   => {
328             # NOTE:
329             # we just alias the original method
330             # rather than re-produce it here
331             'method_metaclass' => \&Class::MOP::Class::method_metaclass
332         },
333         default  => 'Class::MOP::Method',
334     ))
335 );
336
337 Class::MOP::Class->meta->add_attribute(
338     Class::MOP::Attribute->new('instance_metaclass' => (
339         reader   => {
340             # NOTE: we need to do this in order
341             # for the instance meta-object to
342             # not fall into meta-circular death
343             #
344             # we just alias the original method
345             # rather than re-produce it here
346             'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
347         },
348         default  => 'Class::MOP::Instance',
349     ))
350 );
351
352 # NOTE:
353 # we don't actually need to tie the knot with
354 # Class::MOP::Class here, it is actually handled
355 # within Class::MOP::Class itself in the
356 # construct_class_instance method.
357
358 ## --------------------------------------------------------
359 ## Class::MOP::Attribute
360
361 Class::MOP::Attribute->meta->add_attribute(
362     Class::MOP::Attribute->new('name' => (
363         reader   => {
364             # NOTE: we need to do this in order
365             # for the instance meta-object to
366             # not fall into meta-circular death
367             #
368             # we just alias the original method
369             # rather than re-produce it here
370             'name' => \&Class::MOP::Attribute::name
371         }
372     ))
373 );
374
375 Class::MOP::Attribute->meta->add_attribute(
376     Class::MOP::Attribute->new('associated_class' => (
377         reader   => {
378             # NOTE: we need to do this in order
379             # for the instance meta-object to
380             # not fall into meta-circular death
381             #
382             # we just alias the original method
383             # rather than re-produce it here
384             'associated_class' => \&Class::MOP::Attribute::associated_class
385         }
386     ))
387 );
388
389 Class::MOP::Attribute->meta->add_attribute(
390     Class::MOP::Attribute->new('accessor' => (
391         reader    => { 'accessor'     => \&Class::MOP::Attribute::accessor     },
392         predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
393     ))
394 );
395
396 Class::MOP::Attribute->meta->add_attribute(
397     Class::MOP::Attribute->new('reader' => (
398         reader    => { 'reader'     => \&Class::MOP::Attribute::reader     },
399         predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
400     ))
401 );
402
403 Class::MOP::Attribute->meta->add_attribute(
404     Class::MOP::Attribute->new('initializer' => (
405         reader    => { 'initializer'     => \&Class::MOP::Attribute::initializer     },
406         predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
407     ))
408 );
409
410 Class::MOP::Attribute->meta->add_attribute(
411     Class::MOP::Attribute->new('writer' => (
412         reader    => { 'writer'     => \&Class::MOP::Attribute::writer     },
413         predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
414     ))
415 );
416
417 Class::MOP::Attribute->meta->add_attribute(
418     Class::MOP::Attribute->new('predicate' => (
419         reader    => { 'predicate'     => \&Class::MOP::Attribute::predicate     },
420         predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate },
421     ))
422 );
423
424 Class::MOP::Attribute->meta->add_attribute(
425     Class::MOP::Attribute->new('clearer' => (
426         reader    => { 'clearer'     => \&Class::MOP::Attribute::clearer     },
427         predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer },
428     ))
429 );
430
431 Class::MOP::Attribute->meta->add_attribute(
432     Class::MOP::Attribute->new('builder' => (
433         reader    => { 'builder'     => \&Class::MOP::Attribute::builder     },
434         predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder },
435     ))
436 );
437
438 Class::MOP::Attribute->meta->add_attribute(
439     Class::MOP::Attribute->new('init_arg' => (
440         reader    => { 'init_arg'     => \&Class::MOP::Attribute::init_arg     },
441         predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg },
442     ))
443 );
444
445 Class::MOP::Attribute->meta->add_attribute(
446     Class::MOP::Attribute->new('default' => (
447         # default has a custom 'reader' method ...
448         predicate => { 'has_default' => \&Class::MOP::Attribute::has_default },
449     ))
450 );
451
452 Class::MOP::Attribute->meta->add_attribute(
453     Class::MOP::Attribute->new('associated_methods' => (
454         reader   => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods },
455         default  => sub { [] }
456     ))
457 );
458
459 Class::MOP::Attribute->meta->add_method('clone' => sub {
460     my $self  = shift;
461     $self->meta->clone_object($self, @_);
462 });
463
464 ## --------------------------------------------------------
465 ## Class::MOP::Method
466 Class::MOP::Method->meta->add_attribute(
467     Class::MOP::Attribute->new('body' => (
468         reader   => { 'body' => \&Class::MOP::Method::body },
469     ))
470 );
471
472 Class::MOP::Method->meta->add_attribute(
473     Class::MOP::Attribute->new('associated_metaclass' => (
474         reader   => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass },
475     ))
476 );
477
478 Class::MOP::Method->meta->add_attribute(
479     Class::MOP::Attribute->new('package_name' => (
480         reader   => { 'package_name' => \&Class::MOP::Method::package_name },
481     ))
482 );
483
484 Class::MOP::Method->meta->add_attribute(
485     Class::MOP::Attribute->new('name' => (
486         reader   => { 'name' => \&Class::MOP::Method::name },
487     ))
488 );
489
490 Class::MOP::Method->meta->add_method('clone' => sub {
491     my $self  = shift;
492     $self->meta->clone_object($self, @_);
493 });
494
495 ## --------------------------------------------------------
496 ## Class::MOP::Method::Wrapped
497
498 # NOTE:
499 # the way this item is initialized, this
500 # really does not follow the standard
501 # practices of attributes, but we put
502 # it here for completeness
503 Class::MOP::Method::Wrapped->meta->add_attribute(
504     Class::MOP::Attribute->new('modifier_table')
505 );
506
507 ## --------------------------------------------------------
508 ## Class::MOP::Method::Generated
509
510 Class::MOP::Method::Generated->meta->add_attribute(
511     Class::MOP::Attribute->new('is_inline' => (
512         reader   => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline },
513         default  => 0, 
514     ))
515 );
516
517 ## --------------------------------------------------------
518 ## Class::MOP::Method::Accessor
519
520 Class::MOP::Method::Accessor->meta->add_attribute(
521     Class::MOP::Attribute->new('attribute' => (
522         reader   => {
523             'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
524         },
525     ))
526 );
527
528 Class::MOP::Method::Accessor->meta->add_attribute(
529     Class::MOP::Attribute->new('accessor_type' => (
530         reader   => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
531     ))
532 );
533
534 ## --------------------------------------------------------
535 ## Class::MOP::Method::Constructor
536
537 Class::MOP::Method::Constructor->meta->add_attribute(
538     Class::MOP::Attribute->new('options' => (
539         reader   => {
540             'options' => \&Class::MOP::Method::Constructor::options
541         },
542         default  => sub { +{} }
543     ))
544 );
545
546 Class::MOP::Method::Constructor->meta->add_attribute(
547     Class::MOP::Attribute->new('associated_metaclass' => (
548         init_arg => "metaclass", # FIXME alias and rename
549         reader   => {
550             'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
551         },
552     ))
553 );
554
555 ## --------------------------------------------------------
556 ## Class::MOP::Instance
557
558 # NOTE:
559 # these don't yet do much of anything, but are just
560 # included for completeness
561
562 Class::MOP::Instance->meta->add_attribute(
563     Class::MOP::Attribute->new('associated_metaclass',
564         reader   => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
565     ),
566 );
567
568 Class::MOP::Instance->meta->add_attribute(
569     Class::MOP::Attribute->new('_class_name',
570         init_arg => undef,
571         reader   => { _class_name => \&Class::MOP::Instance::_class_name },
572         #lazy     => 1, # not yet supported by Class::MOP but out our version does it anyway
573         #default  => sub { $_[0]->associated_metaclass->name },
574     ),
575 );
576
577 Class::MOP::Instance->meta->add_attribute(
578     Class::MOP::Attribute->new('attributes',
579         reader   => { attributes => \&Class::MOP::Instance::get_all_attributes },
580     ),
581 );
582
583 Class::MOP::Instance->meta->add_attribute(
584     Class::MOP::Attribute->new('slots',
585         reader   => { slots => \&Class::MOP::Instance::slots },
586     ),
587 );
588
589 Class::MOP::Instance->meta->add_attribute(
590     Class::MOP::Attribute->new('slot_hash',
591         reader   => { slot_hash => \&Class::MOP::Instance::slot_hash },
592     ),
593 );
594
595
596 # we need the meta instance of the meta instance to be created now, in order
597 # for the constructor to be able to use it
598 Class::MOP::Instance->meta->get_meta_instance;
599
600 # pretend the add_method never happenned. it hasn't yet affected anything
601 undef Class::MOP::Instance->meta->{_package_cache_flag};
602
603 ## --------------------------------------------------------
604 ## Now close all the Class::MOP::* classes
605
606 # NOTE:
607 # we don't need to inline the
608 # constructors or the accessors
609 # this only lengthens the compile
610 # time of the MOP, and gives us
611 # no actual benefits.
612
613 $_->meta->make_immutable(
614     inline_constructor  => 1,
615     replace_constructor => 1,
616     constructor_name    => "_new",
617     inline_accessors => 0,
618 ) for qw/
619     Class::MOP::Package
620     Class::MOP::Module
621     Class::MOP::Class
622
623     Class::MOP::Attribute
624     Class::MOP::Method
625     Class::MOP::Instance
626
627     Class::MOP::Object
628
629     Class::MOP::Method::Generated
630
631     Class::MOP::Method::Accessor
632     Class::MOP::Method::Constructor
633     Class::MOP::Method::Wrapped
634 /;
635
636 1;
637
638 __END__
639
640 =pod
641
642 =head1 NAME
643
644 Class::MOP - A Meta Object Protocol for Perl 5
645
646 =head1 DESCRIPTON
647
648 This module is a fully functioning meta object protocol for the
649 Perl 5 object system. It makes no attempt to change the behavior or
650 characteristics of the Perl 5 object system, only to create a
651 protocol for its manipulation and introspection.
652
653 That said, it does attempt to create the tools for building a rich
654 set of extensions to the Perl 5 object system. Every attempt has been
655 made for these tools to keep to the spirit of the Perl 5 object
656 system that we all know and love.
657
658 This documentation is admittedly sparse on details, as time permits
659 I will try to improve them. For now, I suggest looking at the items
660 listed in the L<SEE ALSO> section for more information. In particular
661 the book "The Art of the Meta Object Protocol" was very influential
662 in the development of this system.
663
664 =head2 What is a Meta Object Protocol?
665
666 A meta object protocol is an API to an object system.
667
668 To be more specific, it is a set of abstractions of the components of
669 an object system (typically things like; classes, object, methods,
670 object attributes, etc.). These abstractions can then be used to both
671 inspect and manipulate the object system which they describe.
672
673 It can be said that there are two MOPs for any object system; the
674 implicit MOP, and the explicit MOP. The implicit MOP handles things
675 like method dispatch or inheritance, which happen automatically as
676 part of how the object system works. The explicit MOP typically
677 handles the introspection/reflection features of the object system.
678 All object systems have implicit MOPs, without one, they would not
679 work. Explict MOPs however as less common, and depending on the
680 language can vary from restrictive (Reflection in Java or C#) to
681 wide open (CLOS is a perfect example).
682
683 =head2 Yet Another Class Builder!! Why?
684
685 This is B<not> a class builder so much as it is a I<class builder
686 B<builder>>. My intent is that an end user does not use this module
687 directly, but instead this module is used by module authors to
688 build extensions and features onto the Perl 5 object system.
689
690 =head2 Who is this module for?
691
692 This module is specifically for anyone who has ever created or
693 wanted to create a module for the Class:: namespace. The tools which
694 this module will provide will hopefully make it easier to do more
695 complex things with Perl 5 classes by removing such barriers as
696 the need to hack the symbol tables, or understand the fine details
697 of method dispatch.
698
699 =head2 What changes do I have to make to use this module?
700
701 This module was designed to be as unintrusive as possible. Many of
702 its features are accessible without B<any> change to your existsing
703 code at all. It is meant to be a compliment to your existing code and
704 not an intrusion on your code base. Unlike many other B<Class::>
705 modules, this module B<does not> require you subclass it, or even that
706 you C<use> it in within your module's package.
707
708 The only features which requires additions to your code are the
709 attribute handling and instance construction features, and these are
710 both completely optional features. The only reason for this is because
711 Perl 5's object system does not actually have these features built
712 in. More information about this feature can be found below.
713
714 =head2 A Note about Performance?
715
716 It is a common misconception that explict MOPs are performance drains.
717 But this is not a universal truth at all, it is an side-effect of
718 specific implementations. For instance, using Java reflection is much
719 slower because the JVM cannot take advantage of any compiler
720 optimizations, and the JVM has to deal with much more runtime type
721 information as well. Reflection in C# is marginally better as it was
722 designed into the language and runtime (the CLR). In contrast, CLOS
723 (the Common Lisp Object System) was built to support an explicit MOP,
724 and so performance is tuned for it.
725
726 This library in particular does it's absolute best to avoid putting
727 B<any> drain at all upon your code's performance. In fact, by itself
728 it does nothing to affect your existing code. So you only pay for
729 what you actually use.
730
731 =head2 About Metaclass compatibility
732
733 This module makes sure that all metaclasses created are both upwards
734 and downwards compatible. The topic of metaclass compatibility is
735 highly esoteric and is something only encountered when doing deep and
736 involved metaclass hacking. There are two basic kinds of metaclass
737 incompatibility; upwards and downwards.
738
739 Upwards metaclass compatibility means that the metaclass of a
740 given class is either the same as (or a subclass of) all of the
741 class's ancestors.
742
743 Downward metaclass compatibility means that the metaclasses of a
744 given class's anscestors are all either the same as (or a subclass
745 of) that metaclass.
746
747 Here is a diagram showing a set of two classes (C<A> and C<B>) and
748 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
749 metaclass compatibility both upwards and downwards.
750
751     +---------+     +---------+
752     | Meta::A |<----| Meta::B |      <....... (instance of  )
753     +---------+     +---------+      <------- (inherits from)
754          ^               ^
755          :               :
756     +---------+     +---------+
757     |    A    |<----|    B    |
758     +---------+     +---------+
759
760 As I said this is a highly esoteric topic and one you will only run
761 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
762 are interested in why this is an issue see the paper
763 I<Uniform and safe metaclass composition> linked to in the
764 L<SEE ALSO> section of this document.
765
766 =head2 Using custom metaclasses
767
768 Always use the metaclass pragma when using a custom metaclass, this
769 will ensure the proper initialization order and not accidentely
770 create an incorrect type of metaclass for you. This is a very rare
771 problem, and one which can only occur if you are doing deep metaclass
772 programming. So in other words, don't worry about it.
773
774 =head1 PROTOCOLS
775
776 The protocol is divided into 4 main sub-protocols:
777
778 =over 4
779
780 =item The Class protocol
781
782 This provides a means of manipulating and introspecting a Perl 5
783 class. It handles all of symbol table hacking for you, and provides
784 a rich set of methods that go beyond simple package introspection.
785
786 See L<Class::MOP::Class> for more details.
787
788 =item The Attribute protocol
789
790 This provides a consistent represenation for an attribute of a
791 Perl 5 class. Since there are so many ways to create and handle
792 attributes in Perl 5 OO, this attempts to provide as much of a
793 unified approach as possible, while giving the freedom and
794 flexibility to subclass for specialization.
795
796 See L<Class::MOP::Attribute> for more details.
797
798 =item The Method protocol
799
800 This provides a means of manipulating and introspecting methods in
801 the Perl 5 object system. As with attributes, there are many ways to
802 approach this topic, so we try to keep it pretty basic, while still
803 making it possible to extend the system in many ways.
804
805 See L<Class::MOP::Method> for more details.
806
807 =item The Instance protocol
808
809 This provides a layer of abstraction for creating object instances. 
810 Since the other layers use this protocol, it is relatively easy to 
811 change the type of your instances from the default HASH ref to other
812 types of references. Several examples are provided in the F<examples/> 
813 directory included in this distribution.
814
815 See L<Class::MOP::Instance> for more details.
816
817 =back
818
819 =head1 FUNCTIONS
820
821 =head2 Constants
822
823 =over 4
824
825 =item I<IS_RUNNING_ON_5_10>
826
827 We set this constant depending on what version perl we are on, this 
828 allows us to take advantage of new 5.10 features and stay backwards 
829 compat.
830
831 =item I<HAVE_ISAREV>
832
833 Whether or not C<mro> provides C<get_isarev>, a much faster way to get all the
834 subclasses of a certain class.
835
836 =back
837
838 =head2 Utility functions
839
840 =over 4
841
842 =item B<load_class ($class_name)>
843
844 This will load a given C<$class_name> and if it does not have an
845 already initialized metaclass, then it will intialize one for it.
846 This function can be used in place of tricks like 
847 C<eval "use $module"> or using C<require>.
848
849 =item B<is_class_loaded ($class_name)>
850
851 This will return a boolean depending on if the C<$class_name> has
852 been loaded.
853
854 NOTE: This does a basic check of the symbol table to try and
855 determine as best it can if the C<$class_name> is loaded, it
856 is probably correct about 99% of the time.
857
858 =item B<check_package_cache_flag ($pkg)>
859
860 This will return an integer that is managed by C<Class::MOP::Class>
861 to determine if a module's symbol table has been altered. 
862
863 In Perl 5.10 or greater, this flag is package specific. However in 
864 versions prior to 5.10, this will use the C<PL_sub_generation> variable
865 which is not package specific. 
866
867 =item B<get_code_info ($code)>
868
869 This function returns two values, the name of the package the C<$code> 
870 is from and the name of the C<$code> itself. This is used by several 
871 elements of the MOP to detemine where a given C<$code> reference is from.
872
873 =item B<subname ($name, $code)>
874
875 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
876
877 If possible, we will load the L<Sub::Name> module and this will function 
878 as C<Sub::Name::subname> does, otherwise it will just return the C<$code>
879 argument.
880
881 =item B<in_global_destruction>
882
883 If L<Devel::GlobalDestruction> is available, this returns true under global
884 destruction.
885
886 Otherwise it's a constant returning false.
887
888 =back
889
890 =head2 Metaclass cache functions
891
892 Class::MOP holds a cache of metaclasses, the following are functions
893 (B<not methods>) which can be used to access that cache. It is not
894 recommended that you mess with this, bad things could happen. But if
895 you are brave and willing to risk it, go for it.
896
897 =over 4
898
899 =item B<get_all_metaclasses>
900
901 This will return an hash of all the metaclass instances that have
902 been cached by B<Class::MOP::Class> keyed by the package name.
903
904 =item B<get_all_metaclass_instances>
905
906 This will return an array of all the metaclass instances that have
907 been cached by B<Class::MOP::Class>.
908
909 =item B<get_all_metaclass_names>
910
911 This will return an array of all the metaclass names that have
912 been cached by B<Class::MOP::Class>.
913
914 =item B<get_metaclass_by_name ($name)>
915
916 This will return a cached B<Class::MOP::Class> instance of nothing
917 if no metaclass exist by that C<$name>.
918
919 =item B<store_metaclass_by_name ($name, $meta)>
920
921 This will store a metaclass in the cache at the supplied C<$key>.
922
923 =item B<weaken_metaclass ($name)>
924
925 In rare cases it is desireable to store a weakened reference in 
926 the metaclass cache. This function will weaken the reference to 
927 the metaclass stored in C<$name>.
928
929 =item B<does_metaclass_exist ($name)>
930
931 This will return true of there exists a metaclass stored in the 
932 C<$name> key and return false otherwise.
933
934 =item B<remove_metaclass_by_name ($name)>
935
936 This will remove a the metaclass stored in the C<$name> key.
937
938 =back
939
940 =head1 SEE ALSO
941
942 =head2 Books
943
944 There are very few books out on Meta Object Protocols and Metaclasses
945 because it is such an esoteric topic. The following books are really
946 the only ones I have found. If you know of any more, B<I<please>>
947 email me and let me know, I would love to hear about them.
948
949 =over 4
950
951 =item "The Art of the Meta Object Protocol"
952
953 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
954
955 =item "Putting MetaClasses to Work"
956
957 =item "Smalltalk: The Language"
958
959 =back
960
961 =head2 Papers
962
963 =over 4
964
965 =item Uniform and safe metaclass composition
966
967 An excellent paper by the people who brought us the original Traits paper.
968 This paper is on how Traits can be used to do safe metaclass composition,
969 and offers an excellent introduction section which delves into the topic of
970 metaclass compatibility.
971
972 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
973
974 =item Safe Metaclass Programming
975
976 This paper seems to precede the above paper, and propose a mix-in based
977 approach as opposed to the Traits based approach. Both papers have similar
978 information on the metaclass compatibility problem space.
979
980 L<http://citeseer.ist.psu.edu/37617.html>
981
982 =back
983
984 =head2 Prior Art
985
986 =over 4
987
988 =item The Perl 6 MetaModel work in the Pugs project
989
990 =over 4
991
992 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
993
994 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
995
996 =back
997
998 =back
999
1000 =head2 Articles
1001
1002 =over 4
1003
1004 =item CPAN Module Review of Class::MOP
1005
1006 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1007
1008 =back
1009
1010 =head1 SIMILAR MODULES
1011
1012 As I have said above, this module is a class-builder-builder, so it is
1013 not the same thing as modules like L<Class::Accessor> and
1014 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1015 with similar goals to this module. The one I have found which is most
1016 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1017 creates are very different from this modules.
1018
1019 =head1 BUGS
1020
1021 All complex software has bugs lurking in it, and this module is no
1022 exception. If you find a bug please either email me, or add the bug
1023 to cpan-RT.
1024
1025 =head1 ACKNOWLEDGEMENTS
1026
1027 =over 4
1028
1029 =item Rob Kinyon
1030
1031 Thanks to Rob for actually getting the development of this module kick-started.
1032
1033 =back
1034
1035 =head1 AUTHORS
1036
1037 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1038
1039 B<with contributions from:>
1040
1041 Brandon (blblack) Black
1042
1043 Guillermo (groditi) Roditi
1044
1045 Matt (mst) Trout
1046
1047 Rob (robkinyon) Kinyon
1048
1049 Yuval (nothingmuch) Kogman
1050
1051 Scott (konobi) McWhirter
1052
1053 =head1 COPYRIGHT AND LICENSE
1054
1055 Copyright 2006-2008 by Infinity Interactive, Inc.
1056
1057 L<http://www.iinteractive.com>
1058
1059 This library is free software; you can redistribute it and/or modify
1060 it under the same terms as Perl itself.
1061
1062 =cut