[Edinburgh-pm] Dynamic library paths, that work in Test::More & under Apache/mod-perl

Ian Stuart Ian.Stuart at ed.ac.uk
Wed Apr 24 07:40:25 PDT 2013

So, I've some CGI scripts that work just ace...

They pass a few hundred Test::More tests, and respond well as CGI 
scripts.... but there is one flaw: they have hard-coded paths in them

Start with the working case - near the start of the script I have:

   use common::sense;
   use lib "/home/me/project_1/dev/perl5/lib/perl5/site_perl";
   use Apache2::RequestRec;

and this works fine under Apache/mod-perl, and passes the tests I throw 
at it.

Of course, to move this to a testing server, I need to edit the 'use 
lib' path (for each script, obviously) - not clever.... so I want to 
abstract the library path

*First attempt*: rely on PERL5LIB in the environment.
   Running the script from the command line is fine (@INC picks up 
PERL5LIB), but it fails under testing and through apache: 
"Apache2::RequestRec not found" - @INC does not have the stuff from PERL5LIB

*Second attempt*: Add
   PerlSwitches -I/home/me/project_1/dev/perl5/lib/perl5/site_perl

into an httpd conf file - nope: still fails
(and I know the config file is being pulled in as both the cgi-bin and 
scriptAlias' defined in that file are working

*Third attempt*: put something in a BEGIN block:
   BEGIN {
     unshift @INC, $ENV{BASE_APR_DIR}."/perl5/lib/perl5/site_perl";
     warn Dumper(\%ENV);
     warn Dumper(\@INC);
     use Apache2::RequestRec;

%ENV has
   'BASE_API_DIR' => '/home/me/project_1/dev'
but @INC only gets

Any thoughts on how to set @INC dynamically?
... so I can put a file into Source Control, and it will work whatever 
the root path is?


Ian Stuart.
Developer: ORI, RJ-Broker, and OpenDepot.org
Bibliographics and Multimedia Service Delivery team,
The University of Edinburgh.


This email was sent via the University of Edinburgh.

The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

More information about the Edinburgh-pm mailing list