perlapache2mod-perl2

mod_perl2: Use module by its location and not by its name


I am having 2 directories in my Apache webserver.

PerlModule HandlerA
PerlModule HandlerB

<Directory "/var/www/html/testa">
    Options FollowSymLinks
    Order deny,allow
    AllowOverride All

    SetHandler perl-script
    PerlHandler HandlerA
</Directory>

<Directory "/var/www/html/testb">
    Options FollowSymLinks
    Order deny,allow
    AllowOverride All

    SetHandler perl-script
    PerlHandler HandlerB
</Directory>

/testa has HandlerA and /testb has HandlerB.

HandlerA.pm

package HandlerA;

use strict;
use warnings;
use Apache2::Const;
use lib "/etc/apache2/a";
use MyA;
use MyX;

sub handler
{
    my $r = shift;

    my $str = "Handler=A  MyA=" . MyA::foo () . "  MyX=" . MyX::foo ();
    $r->log_error ($str);

    $r->content_type ("text/plain");
    print $str;
    return Apache2::Const::OK;
}
1;

HandlerB.pm

package HandlerB;

use strict;
use warnings;
use Apache2::Const;
use lib "/etc/apache2/b";
use MyB;
use MyX;

sub handler
{
    my $r = shift;

    my $str = "Handler=B  MyB=" . MyB::foo () . "  MyX=" . MyX::foo ();
    $r->log_error ($str);

    $r->content_type ("text/plain");
    print $str;
    return Apache2::Const::OK;
}
1;

In each handler I am using foo of a module. The modules reside in a directory a (for HandlerA) and b (for HandlerB).

a/MyA::foo prints A

b/MyB::foo prints B

a/MyX::foo prints A

b/MyX::foo prints B

Only showing a/MyX.pm

package MyX;

sub foo
{
    return "A";
}
1;

Because MyA and MyB differ by name it is working fine that HandlerA uses the correct foo.

But thats not the case with MyX.

How can I make is possible to use a/MyX.pm in HandlerA and b/MyX.pm in HandlerB? So it should use the module not by name but by its file-location.

Output of Handler A should be

Handler=A MyA=A MyX=A

Output of HandlerB should be

Handler=B MyB=B MyX=B

MyX is not always working as expected and it gets mixed up.


Solution

  • As Grinzz pinted out, @INC and package namespaces are global - so the solution is to add a different perl interpreter for each directory.

    According to the documentation: https://perl.apache.org/docs/2.0/user/config/config.html#C_Parent_ this should work at least in a Location-Directive.