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