How do I dynamically change the Perl module path?
module, perl
Solution
You need to use a `BEGIN{}` block so your `if`-`else` code will be run at compile-time:
BEGIN {
if ($new) { unshift @INC, "newdir"; }
else { unshift @INC, "olddir"; }
}
use module;
You can also set the `PERL5LIB` environment variable so you wouldn't have to do this kind of configuration in the script.
Problem
I have two different versions of a Perl module. Currently, the crucial script uses the version of the module specified by an environment variable and the system relied on different tasks being run by different users. The user's environment would determine which version of the Perl module was used. Now I would like to change this to the version being specified inside the Perl script, i.e. depending on the options passed. Unfortunately, code like this: ``` if ($new){ use lib "newdir"; } else{ use lib "olddir"; } use module; ``` doesn't work. Perl simply appends newdir and then olddir to `@INC` and then runs the script. How do I dynamically specify which module to use?