[Edinburgh-pm] Dynamic library paths, that work in Test::More & under Apache/mod-perl
Marco Fontani
fontani at gmail.com
Wed Apr 24 08:28:15 PDT 2013
On Wed, Apr 24, 2013 at 3:40 PM, Ian Stuart <Ian.Stuart at ed.ac.uk> 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;
> [...]
> 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?
Yes, assuming you have a "sane" directory structure in your repository:
repo/lib
repo/cgi-bin
on your repo/cgi-bin/foo.cgi:
use FindBin; # https://metacpan.org/module/FindBin
use lib "$FindBin::Bin/../lib";
If the structure isn't like that, use the necessary amount of "../" to
make it work.
Example showing the script "works" (i.e. finds the right lib) no
matter where you call it from:
(~) $ mkdir -p /tmp/ian/{lib,cgi-bin}; cd /tmp/ian
(/tmp/ian) $ echo "package A; warn 'Loaded A'; 1;" > lib/A.pm
(/tmp/ian) $ echo 'use FindBin;use lib "$FindBin::Bin/../lib"; use A;
print "works!\n";' > cgi-bin/foo.cgi
(/tmp/ian) $ perl cgi-bin/foo.cgi
Loaded A at /private/tmp/ian/cgi-bin/../lib/A.pm line 1.
works!
(/tmp/ian) $ cd /tmp
(/tmp) $ perl cgi-bin/foo.cgi
Loaded A at /private/tmp/ian/cgi-bin/../lib/A.pm line 1.
works!
(/tmp) $ cd
(~) $ perl cgi-bin/foo.cgi
Loaded A at /private/tmp/ian/cgi-bin/../lib/A.pm line 1.
works!
--
Marco Fontani
More information about the Edinburgh-pm
mailing list