Compiling a package into a namespace

Simon Wistow simon at thegestalt.org
Fri Apr 11 19:52:56 BST 2008


This is very possibly one of those "You're asking the wrong question" 
questions but what the hell.

I have a mod_perl app and I want to load a package called 'foo' defined 
in ~/simon/a/foo.pm. However I also want to load a package called 'foo' 
defined in ~/simon/b/foo.pm 

This will cause problems. 

So I'm wondering if it's possible to load ~/simon/a/foo.pm as a package 
named hash('~/simon/a/').'foo' or similar.

The one way I can think of doing it is reading in foo.pm, doing 

	s!^package\s+([^\s;]+)\s*;!package ${path_hash}::${1};!smg;

 and then evaling it but that seems ... icky. And error prone especially 
if someone then does

	my $foo = foo->new;

unless I provide a method 

	my $foo = lookup_class('foo')->new;

where lookup_class just does return "${path_hash}::$_[0]" unless $_[0] 
is in %INC. Still ick though.

Of course then there's PPI but maybe that's feels hacky too.

So then I'm left with two other options - don't do it this way do it 
some way which I have no idea about or just mandate that you can't have 
two packages called 'foo'.

Thoughts?














More information about the london.pm mailing list