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