Setting PERL5LIB
Solution 1
Setting PERL5LIB
at runtime will not affect Perl's search path. You need to export the variable before executing the interpreter.
Alternatively you can modify @INC
at compile time (also possible to do in a separate script/module):
BEGIN { unshift @INC, "/path/to/dir" }
This is what the lib
pragma does.
Solution 2
You'd do this via 'use lib' rather than manipulating the environment:
use lib '/home/perl5';
That could be in a separate file that you 'require' in.
Solution 3
PERL5INC
is a shell environment variable, so you wouldn't set it inside your Perl program (normally) but instead specify it before invoking Perl. The below is a shell command where I've used PERL5LIB
to instruct prove
to find a Perl module residing in ~/OnePop
:
$ PERL5LIB=~/OnePop prove -l t
... PERL5LIB is unset here ....
When a command is preceded by a variable assignment like this, the shell sets and exports the variable (PERL5LIB
) to that command, but after that the variable will be unset again. You can also set the variable in the shell, so that all subsequent commands will inherit it.
$ export PERL5LIB=~/OnePop
...
$ prove -l t
... PERL5LIB continues to be set here ...
If you forget the export
keyword in the above example (i.e. assigns the value using PERL5LIB=~/OnePop
on a separate line) the variable will be set in the shell, but it will not be inherited by any commands you run (meaning that prove
will not be able to see it).
Finally, if you wanted to set the environment PERL5LIB
variable from inside a Perl program you'd have to write it like this:
$ENV{PERL5LIB} = glob("~/OnePop"); # glob() expands the tilde
system(qw( prove -l t ));
Though, as other have pointed out, if you want to specify the include path from inside Perl it is easier/better to use use lib $PATH
.
Related videos on Youtube
CMS
Updated on May 02, 2020Comments
-
CMS almost 4 years
Can I set PERL5LIB in a seperate script and call that script in other scripts? How do I do it? And how would it affect the script in which it is used?
Thanks.
-
Mark about 13 yearsThis doesn't work for me, I think because the PERL5LIB environment variable is processed by the interpreter before the script is executed, so @INC isn't modified.