Pinunki wrote:
[...]
> I have tried using Apache::Register and
Apache::PerlRun but am having problems due to the fact
that each web site refers to their own copy of my
module called Site.pm (to contain functions, global
variables etc). The problem is that the my scripts get
confused and use variables from the other site's
modules randomly.
[...]

If your module which has an indentical name but different contents and it never calls functions and accesses variables by prefixing the module name (e.g. Site::foo and $Site::bar), i.e. everything is exported, you could hack Apache::Registry to compile the different files into different package names and do the importing for you. Here is the proof of concept (untested!) code. All it does, is reading the file (Site.pm in your case), prepending the string

'package foo_bar_tar_lib1_Site_pm;'

to the contents of the file, compiling the module via eval and then calling import() to import all the stuff as it did before. Notice that you do have to change your scripts to call the trickery my_use().

(Alternatively you could try to play with aliasing of the namespaces, instead of mangling the file contents, but this could introduce problems...)

my_script.pl:
-------------
my @imports();
Apache::Registry::my_use('Site.pm', @imports);

Site.pm
-------
# no package declaration!
# the rest is as usual Exporter, vars, subs
# blah, blah
1;

Registry.pm
-----------

package Apache::Registry;

# the normal Registry stuff ...

# then the extra stuff:

use File::Spec::Functions;

sub my_use {
my($module, @import) = @_;

my $file = catfile Apache->request->filename, $module;

my $namespace = make_namespace($file);

compile($namespace, $file) or die "failed to compile: $@";

$namespace->import(@import);

}

sub make_namespace {
my ($path) = @_;

$path =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;

# make sure that the sub-package doesn't start with a digit
$path =~ s/^(\d)/_$1/;

return $path;
}

sub compile {
my ($namespace, $file) = @_;

open my $fh, $file;
local $/;
my $data = <$fh>;
close $fh;

{ eval "package $namespace;\n $data"; }
return $@ ? 0 : 1;
}

1;


__________________________________________________________________
Stas Bekman JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/ mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org http://ticketmaster.com



Reply via email to