0.02 release
Stevan Little [Mon, 8 Aug 2005 04:44:06 +0000 (04:44 +0000)]
ChangeLog
MANIFEST
lib/Class/C3.pm
t/01_MRO.t
t/02_MRO.t
t/03_MRO.t
t/20_reinitialize.t [new file with mode: 0644]

index 4d8d8ae..5e76252 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,10 +1,19 @@
 Revision history for Perl extension Class::C3.
 
-0.02 - Sun, Aug 7, 2005
-    - code refactoring and comments added
+0.02 - Mon, Aug 8, 2005
+    - code refactoring
+        - many comments added
     - added many more tests 
+        - most of the tests from Perl6::MetaModel moved over
+        - tested loading modules with `use` as well as the 
+          inline package definition
     - added optional 'c3' pragma
+        - this is not installed and can be found in opt/
+    - added `uninitialize` function to remove C3 dispatch ordering 
+        - added tests for this
+    - added `reinitialize` function to reload C3 dispatch ordering     
+        - added tests for this
 
 0.01 - Sun, Aug 7, 2005
     - initial release of module
-    - some code and test based on previous Perl6::MetaModel work
\ No newline at end of file
+    - some code and tests based on previous Perl6::MetaModel work
\ No newline at end of file
index 9540575..6820463 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -11,6 +11,7 @@ t/03_MRO.t
 t/04_MRO.t
 t/05_MRO.t
 t/10_Inconsistent_hierarchy.t
+t/20_reinitialize.t
 t/pod.t
 t/pod_coverage.t
 t/lib/A.pm
index 3768c47..385fc1d 100644 (file)
@@ -23,9 +23,8 @@ our $VERSION = '0.02';
 #
 my %MRO;
 
-# use this for debugging ...
+# use these for debugging ...
 sub _dump_MRO_table { %MRO }
-
 our $TURN_OFF_C3 = 0;
 
 sub import {
@@ -58,6 +57,19 @@ sub initialize {
     _apply_method_dispatch_tables();
 }
 
+sub uninitialize {
+    # why bother if we don't have anything ...
+    return unless keys %MRO;    
+    _remove_method_dispatch_tables();    
+}
+
+sub reinitialize {
+    uninitialize();
+    # clean up the %MRO before we re-initialize
+    $MRO{$_} = undef foreach keys %MRO;
+    initialize();
+}
+
 ## functions for applying C3 to classes
 
 sub _calculate_method_dispatch_tables {
@@ -104,6 +116,20 @@ sub _apply_method_dispatch_table {
     }    
 }
 
+sub _remove_method_dispatch_tables {
+    foreach my $class (keys %MRO) {
+        _remove_method_dispatch_table($class);
+    }       
+}
+
+sub _remove_method_dispatch_table {
+    my $class = shift;
+    no strict 'refs';
+    foreach my $method (keys %{$MRO{$class}->{methods}}) {
+        delete ${"${class}::"}{$method};
+    }   
+}
+
 ## functions for calculating C3 MRO
 
 # this function is a perl-port of the 
@@ -273,9 +299,20 @@ This can be used to initalize the C3 method dispatch tables. You need to call th
 under mod_perl, or in any other environment which does not run the INIT phase of the perl compiler.
 
 NOTE: 
-This can B<not> be used to re-load the dispatch tables for all classes. This is because it does not first
-return the classes to their virginal state, which would need to happen in order for the dispatch tables
-to be properly reloaded.
+This can B<not> be used to re-load the dispatch tables for all classes. Use C<reinitialize> for that.
+
+=item B<uninitialize>
+
+Calling this function results in the removal of all cached methods, and the restoration of the old Perl 5
+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.
 
 =back
 
@@ -302,14 +339,16 @@ next most appropriate method in the MRO.
 
 It is the author's opinion that changing C<@ISA> at runtime is pure insanity anyway. However, people
 do it, so I must caveat. Any changes to the C<@ISA> will not be reflected in the MRO calculated by this
-module, and therefor probably won't even show up. I am considering some kind of C<recalculateMRO> function
-which can be used to recalculate the MRO on demand at runtime, but that is still off in the future.
+module, and therefor probably won't even show up. If you do this, you will need to call C<reinitialize> 
+in order to recalulate B<all> method dispatch tables. See the C<reinitialize> documentation and an example
+in F<t/20_reinitialize.t> for more information.
 
 =item Adding/deleting methods from class symbol tables.
 
 This module calculates the MRO for each requested class during the INIT phase by interogatting the symbol
 tables of said classes. So any symbol table manipulation which takes place after our INIT phase is run will
-not be reflected in the calculated MRO.
+not be reflected in the calculated MRO. Just as with changing the C<@ISA>, you will need to call 
+C<reinitialize> for any changes you make to take effect.
 
 =back
 
@@ -321,19 +360,11 @@ not be reflected in the calculated MRO.
 
 You can never have enough tests :)
 
-I need to convert the other MRO and class-precendence-list related tests from the Perl6-MetaModel (see link
-in L<SEE ALSO>). In addition, I need to add some method checks to these tests as well.
-
 =item call-next-method / NEXT:: / next METHOD
 
 I am contemplating some kind of psudeo-package which can dispatch to the next most relevant method in the 
 MRO. This should not be too hard to implement when the time comes.
 
-=item recalculateMRO
-
-This being Perl, it would be remiss of me to force people to close thier classes at runtime. So I need to 
-develop a means for recalculating the MRO for a given class. 
-
 =back
 
 =head1 SEE ALSO
index 40f792f..04ad0e8 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 5;
+use Test::More tests => 11;
 
 BEGIN {
     use_ok('Class::C3');
@@ -53,6 +53,21 @@ is_deeply(
     '... got the right MRO for Diamond_D');
 
 is(Diamond_D->hello, 'Diamond_C::hello', '... method resolved itself as expected');
+
 is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
+is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
+
+# now undo the C3
+Class::C3::uninitialize();
+
+is(Diamond_D->hello, 'Diamond_A::hello', '... old method resolution has been restored');
+
+is(Diamond_D->can('hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored');
+is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_A::hello', '... can(method) resolution has been restored');
+
+Class::C3::initialize();
+
+is(Diamond_D->hello, 'Diamond_C::hello', '... C3 method restored itself as expected');
 
-is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... can(method) resolved itself as expected');
\ No newline at end of file
+is(Diamond_D->can('hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected');
+is(UNIVERSAL::can("Diamond_D", 'hello')->(), 'Diamond_C::hello', '... C3 can(method) restored itself as expected');
index 9008f20..f04b193 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 11;
+use Test::More tests => 15;
 
 BEGIN {
     use_ok('Class::C3');
@@ -122,4 +122,13 @@ is(Test::A->can('C_or_D')->(), 'Test::C', '... can got the expected method outpu
 is(Test::A->C_or_E, 'Test::C', '... got the expected method output');
 is(Test::A->can('C_or_E')->(), 'Test::C', '... can got the expected method output');
 
+# remove the C3
+Class::C3::uninitialize();
+
+is(Test::A->C_or_D, 'Test::D', '...  old method resolution has been restored');
+is(Test::A->can('C_or_D')->(), 'Test::D', '...  old can(method) resolution has been restored');
+
+is(Test::A->C_or_E, 'Test::E', '...  old method resolution has been restored');
+is(Test::A->can('C_or_E')->(), 'Test::E', '...  old can(method) resolution has been restored');
+
     
\ No newline at end of file
index 0b098cf..f5ce4c7 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 5;
+use Test::More tests => 8;
 
 BEGIN {
     use_ok('Class::C3');
@@ -107,3 +107,9 @@ is(Test::A->O_or_F, 'Test::F', '... got the right method dispatch');
 # would actually call Test::D before Test::C and Test::D is a
 # subclass of Test::C 
 is(Test::A->C_or_D, 'Test::C', '... got the right method dispatch');    
+
+Class::C3::uninitialize();
+
+is(Test::A->O_or_D, 'Test::O', '... old dispatch order is restored');    
+is(Test::A->O_or_F, 'Test::O', '... old dispatch order is restored');   
+is(Test::A->C_or_D, 'Test::D', '... old dispatch order is restored');   
diff --git a/t/20_reinitialize.t b/t/20_reinitialize.t
new file mode 100644 (file)
index 0000000..680276b
--- /dev/null
@@ -0,0 +1,86 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 5;
+
+BEGIN {
+    use_ok('Class::C3');
+    # uncomment this line, and re-run the
+    # test to see the normal p5 dispatch order
+    #$Class::C3::TURN_OFF_C3 = 1;
+}
+
+=pod
+
+Start with this:
+
+   <A>
+  /   \
+<B>   <C>
+  \   /
+   <D>
+
+=cut
+
+{
+    package Diamond_A;
+    use Class::C3; 
+    sub hello { 'Diamond_A::hello' }
+}
+{
+    package Diamond_B;
+    use base 'Diamond_A';
+    use Class::C3;        
+}
+{
+    package Diamond_C;
+    use Class::C3;    
+    use base 'Diamond_A';     
+    sub hello { 'Diamond_C::hello' }
+}
+{
+    package Diamond_D;
+    use base ('Diamond_B', 'Diamond_C');
+    use Class::C3;    
+}
+
+is_deeply(
+    [ Class::C3::calculateMRO('Diamond_D') ],
+    [ qw(Diamond_D Diamond_B Diamond_C Diamond_A) ],
+    '... got the right MRO for Diamond_D');
+
+=pod
+
+Then change it to this:
+
+<E>   <A>
+  \  /   \
+   <B>   <C>
+     \   /
+      <D>
+
+=cut
+
+{
+    package Diamond_E;
+    use Class::C3;      
+    sub hello { 'Diamond_E::hello' }      
+}
+
+{
+    no strict 'refs';
+    unshift @{"Diamond_B::ISA"} => 'Diamond_E';
+}
+
+is_deeply(
+    [ Class::C3::calculateMRO('Diamond_D') ],
+    [ qw(Diamond_D Diamond_B Diamond_E Diamond_C Diamond_A) ],
+    '... got the new MRO for Diamond_D');
+
+is(Diamond_D->hello, 'Diamond_C::hello', '... method still resolves with old MRO');
+
+Class::C3::reinitialize();
+
+is(Diamond_D->hello, 'Diamond_E::hello', '... method resolves with reinitialized MRO');