如何包含位于不同目录中的 Perl 模块? [英] How do I include a Perl module that's in a different directory?

查看:34
本文介绍了如何包含位于不同目录中的 Perl 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何包含位于不同目录中的 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");

推荐答案

将正确的解决方案放在首位,最初来自 这个问题.它是唯一一个相对于 module 目录进行搜索的:

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;

还有许多其他方法可以搜索与当前目录相关的库.您可以使用 -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{} 中以确保在模块搜索之前运行推送:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆