Following is an implementation of "qualified packages", which means that each module (Test1 and Test2) can depend on different variant ("Foo1" and "Foo2") of certain package ("Foo").
It's posted here because I couldn't remember just how many people I agreed to send it to. :-) In any case... enjoy, and see you tomorrow!
use strict;
BEGIN { $^P |= 0x01 }
# The implementation - Dependency maps from packages to their module mappings
# -- maybe just read this out from only.pm or PAR.pm
# -- the next thing is to hijack &require to put the required
# -- packages into alternate namespaces indexed by names
my %dep_map = (
Test1 => { Foo => 'Foo1' },
Test2 => { Foo => 'Foo2' },
);
sub DB::sub {
my $cls = (split(/::/, $DB::sub, 2))[0];
while (my ($k, $v) = each %{ $dep_map{$cls} }) {
no strict 'refs';
%{"$k\::"} = %{"$v\::"};
}
goto &$DB::sub;
}
# The use case
package Foo1; sub new { bless {}, 'Foo1' }
package Foo2; sub new { bless {}, 'Foo2' }
package Test1; sub t1 { Common->foo }
package Test2; sub t2 { Common->foo }
package Common; sub foo { Foo->new }
package main;
warn Test1::t1(); # Foo1 object
warn Test2::t2(); # Foo2 object
Dude!
Posted by: Jesse | 2006.06.27 at 01:04 PM