It is impossible to use two modules with identical names on the same server. Only the first one found in a use( ) or a require( )statement will be loaded and compiled. All subsequent requests to load a module with the same name will be skipped, because Perl will find that there is already an entry for the requested module in the %INC hash.
Let's examine a scenario in which two independent projects in separate directories, projectA and projectB, both need to run on the same server. Both projects use a module with the name MyConfig.pm, but each project has completely different code in its MyConfig.pm module. This is how the projects reside on the filesystem (all located under the directory /home/httpd/perl):
projectA/MyConfig.pm projectA/run.pl projectB/MyConfig.pm projectB/run.pl
Examples Example 6-7, Example 6-9 show some sample code.
use lib qw(.); use MyConfig; print "Content-type: text/plain\n\n"; print "Inside project: ", project_name( );
sub project_name { return 'A'; } 1;
use lib qw(.); use MyConfig; print "Content-type: text/plain\n\n"; print "Inside project: ", project_name( );
sub project_name { return 'B'; } 1;
Both projects contain a script, run.pl, which loads the module MyConfig.pm and prints an indentification message based on the project_name( ) function in the MyConfig.pm module. When a request to /perl/projectA/run.pl is issued, it is supposed to print:
Inside project: A
Similarly, /perl/projectB/run.pl is expected to respond with:
Inside project: B
When tested using single-server mode, only the first one to run will load the MyConfig.pm module, although both run.pl scripts call use MyConfig. When the second script is run, Perl will skip the use MyConfig;statement, because MyConfig.pm is already located in %INC. Perl reports this problem in the error_log:
Undefined subroutine &Apache::ROOT::perl::projectB::run_2epl::project_name called at /home/httpd/perl/projectB/run.pl line 4.
This is because the modules didn't declare a package name, so the project_name( )subroutine was inserted into projectA/run.pl's namespace, Apache::ROOT::perl::projectB::run_2epl. Project B doesn't get to load the module, so it doesn't get the subroutine either!
Note that if a library were used instead of a module (for example, config.pl instead of MyConfig.pm), the behavior would be the same. For both libraries and modules, a file is loaded and its filename is inserted into %INC.
 
Continue to: