[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 08:17:32 PDT 2013


Answered my own question.... (is this some strange variation on 
"teddybear programming? 
[http://blog.adrianbolboaca.ro/2012/12/teddy-bear-pair-programming/])

Anyway.... so long as the libraries are in a known location relative to 
the scripts - eg:
   /home/me/project_1/dev/cgi-bin/myScript
   /home/me/project_1/dev/perl5/lib/perl5/site_perl

then this seems to work:
BEGIN {
   use FindBin;
   use lib "$FindBin::Bin/../perl5/lib/perl5/site_perl";
   use lib "$FindBin::Bin/../testing";
   use Apache2::RequestRec;
}
(I need the testing directory, to pick up the libraries used by the 
test-suite)

On 24/04/13 15:40, Ian Stuart wrote:
> 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
>     '/perl5/lib/perl5/site_perl'
>
>
> 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,
EDINA,
The University of Edinburgh.

http://edina.ac.uk/

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