asdf asdf - 2 months ago 8
Perl Question

Calling script with the same @INC as the parent script

I want to call an external script with

system ($script)
or
do $script
. In my @INC i have some specific modules which I import. How can I call the
$script
and transfer it the same
@INC
?

Script1.pm

#importing some libs
#code
$script = "path_to_script";

system ($script);


Script2.pm

use LibFromScript1@INC;


And I get the Error :


Can't locate LibFromScript1@INC in @INC...

Answer

The easiest way is probably to set the PERL5LIB environment variable. That will add a list of directories to the child process's @INC array

Your code would look something like

$ENV{PERL5LIB} = join ':', @INC;

system $script;

This has the disadvantage that the standard directories will also be added to @INC. It shouldn't cause any problems, but it would be best to set PERL5LIB to just the custom directories if you know them at that point.

Note also that perl will ignore PERL5LIB if you are running under the taint flag.

Comments