make new branch for PurePerl
[gitmo/Class-C3.git] / lib / Class / C3.pm
index 73e8228..a9158ed 100644 (file)
@@ -7,7 +7,7 @@ use warnings;
 use Scalar::Util 'blessed';
 use Algorithm::C3;
 
-our $VERSION = '0.13';
+our $VERSION = '0.14';
 
 # this is our global stash of both 
 # MRO's and method dispatch tables
@@ -29,6 +29,9 @@ our %MRO;
 sub _dump_MRO_table { %MRO }
 our $TURN_OFF_C3 = 0;
 
+# state tracking for initialize()/uninitialize()
+our $_initialized = 0;
+
 sub import {
     my $class = caller();
     # skip if the caller is main::
@@ -45,9 +48,14 @@ sub import {
 sub initialize {
     # why bother if we don't have anything ...
     return unless keys %MRO;
+    if($_initialized) {
+        uninitialize();
+        $MRO{$_} = undef foreach keys %MRO;
+    }
     _calculate_method_dispatch_tables();
     _apply_method_dispatch_tables();
     %next::METHOD_CACHE = ();
+    $_initialized = 1;
 }
 
 sub uninitialize {
@@ -55,14 +63,10 @@ sub uninitialize {
     return unless keys %MRO;    
     _remove_method_dispatch_tables();    
     %next::METHOD_CACHE = ();
+    $_initialized = 0;
 }
 
-sub reinitialize {
-    uninitialize();
-    # clean up the %MRO before we re-initialize
-    $MRO{$_} = undef foreach keys %MRO;
-    initialize();
-}
+sub reinitialize { goto &initialize }
 
 ## functions for applying C3 to classes
 
@@ -358,7 +362,9 @@ any other users other than the L<DBIx::Class> folks). The simplest solution of c
 your own INIT method which calls this function. 
 
 NOTE: 
-This can B<not> be used to re-load the dispatch tables for all classes. Use C<reinitialize> for that.
+
+If C<initialize> detects that C<initialize> has already been executed, it will L</uninitialize> and
+clear the MRO cache first.
 
 =item B<uninitialize>
 
@@ -367,11 +373,7 @@ style dispatch order (depth-first, left-to-right).
 
 =item B<reinitialize>
 
-This effectively calls C<uninitialize> followed by C<initialize> the result of which is a reloading of
-B<all> the calculated C3 dispatch tables. 
-
-It should be noted that if you have a large class library, this could potentially be a rather costly 
-operation.
+This is an alias for L</initialize> above.
 
 =back
 
@@ -562,6 +564,8 @@ C<maybe::next::method>.
 
 Stevan Little, E<lt>stevan@iinteractive.comE<gt>
 
+Brandon L. Black, E<lt>blblack@gmail.comE<gt>
+
 =head1 COPYRIGHT AND LICENSE
 
 Copyright 2005, 2006 by Infinity Interactive, Inc.