Can I use a local::lib if local::lib isn't installed globally and without eval-ing it in shell?

292 Views Asked by At

I have a problem, I want to use local::lib; in a script. But because I need to use this script many places, I don't want to try adding the eval to bashrc, every time I install this script to a server. and I can't get local::lib installed globally (in the default @INC) on the servers. Is there any way I can use local::lib from within the script so that it knows where the module local::lib is without the eval that local::lib recommends and without installing it into a directory in the default @INC on the server?

2

There are 2 best solutions below

1
On BEST ANSWER

Something like this at the top of your script should work, though I have local::lib installed on all my boxes in the regular @INC so it's untested–

use lib "/path/to/local/lib's/lib";
use local::lib "/path/to/specific/local-lib/base/you/want";

I would recommend having local::lib installed the same way, in the default @INC with your main env perl. The various local repositories it can create are not tied to its location proper.

2
On

You need to know at least where local::lib was installed, and you won't get around specifying this, so you need one shell command or environment variable declaration. Assuming you used the default for --bootstrap, this is a minimal example in bash for Perl to find the library:

# persistent for this shell only
export PERL5LIB=$PERL5LIB:$(perl -MConfig -e'print "$ENV{HOME}/perl5/lib/perl5/$Config{archname}:$ENV{HOME}/perl5/lib/perl5"')
perl yourprog
perl yourprog
perl yourprog

or just

# environment for one run of perl only
PERL5LIB=$PERL5LIB:$(perl -MConfig -e'print "$ENV{HOME}/perl5/lib/perl5/$Config{archname}:$ENV{HOME}/perl5/lib/perl5"') perl yourprog

It is safe to simply hard-code the directory names and thus avoid the eval and call to perl.

use local::lib; will find and load the module from the extended @INC. Inspect %INC to see the specific location if you actually need that.

Caveat: You cannot use local::lib now to install additional modules, that needs the rest of the declarations made in the usual eval it prints out.


Setting environment vars from within with the reexecution trick as per urgh below.

use autodie qw(:all);
unless ($ENV{REEXEC}) {
    $ENV{REEXEC} = 1;
    $ENV{PERL5LIB} = …
    exec $^X, $0;
}
# main starts here