Compiling a package into a namespace

Jesse Vincent jesse at
Fri Apr 11 20:56:47 BST 2008

On Apr 11, 2008, at 3:25 PM, Simon Wistow wrote:
> On Fri, Apr 11, 2008 at 08:03:30PM +0100, Dave Cantrell said:
>> which is Just Fine.  Or are you worried about, eg, both of them  
>> defining
>> a subroutine with the same name?
> Unfortunately it's not only probably but guaranteed that they'll both
> have the subroutines named the same.

Ah. I misunderstood. You'll need something a bit more industrial- 
Unsurprisingly, Audrey has a blog post on the topic:

Multiversioning and qualified packages

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  
# -- maybe just read this out from or
# -- 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url :

More information about the mailing list