如何包含位于不同目录中的Perl模块? [英] How do I include a Perl module that's in a different directory?
问题描述
如何包含位于其他目录中的Perl模块?它必须是包含它的模块的相对路径.
How do I include a Perl module that's in a different directory? It needs to be a relative path from the module that's including it.
我尝试过
push ( @INC,"directory_path/more_path");
也
push ( @INC,"directory_path\\more_path");
推荐答案
Putting the right solution first, originally from this question. It's the only one that searches relative to the module directory:
use FindBin; # locate this script
use lib "$FindBin::Bin/.."; # use the parent directory
use yourlib;
还有许多其他搜索相对于 current 目录的库的方法.您可以使用-I参数调用perl,并传递其他模块的目录:
There's many other ways that search for libraries relative to the current directory. You can invoke perl with the -I argument, passing the directory of the other module:
perl -I.. yourscript.pl
您可以在perl脚本顶部附近添加一行:
You can include a line near the top of your perl script:
use lib '..';
您可以在运行脚本之前修改环境变量PERL5LIB:
You can modify the environment variable PERL5LIB before you run the script:
export PERL5LIB=$PERL5LIB:..
push(@INC)策略也可以使用,但是必须将其包装在BEGIN {}中,以确保在模块搜索之前运行push:
The push(@INC) strategy can also work, but it has to be wrapped in BEGIN{} to make sure that the push is run before the module search:
BEGIN {push @INC, '..'}
use yourlib;
这篇关于如何包含位于不同目录中的Perl模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!