在其他Perl模块中使用Perl自定义模块的正确方法
我在我的脚本中使用自定义模块,并且必须将它们存储在Perl lib
目录之外。所以在Perl脚本( *。pl
)中,我使用以下块将它们包含在 @INC
中:
I'm using custom modules in my scripts and have to store them outside of Perl lib
directory. So in Perl scripts (*.pl
) I use the following block to include them in @INC
:
BEGIN {
use FindBin qw($Bin);
push @INC, "$Bin/../ModulesFolder1";
push @INC, "$Bin/../ModulesFolder2";
}
但我还必须在我的其他Perl模块中使用模块( * .pm
),据我所知, FindBin
仅适用于脚本。所以我将该块更改为:
But I also have to use modules inside of my other Perl modules (*.pm
), and as I understand FindBin
works for scripts only. So I change that block to:
BEGIN {
push @INC, catdir( dirname( $INC{'ThisModule.pm'} ), qw( .. ModulesFolder1 ) );
push @INC, catdir( dirname( $INC{'ThisModule.pm'} ), qw( .. ModulesFolder2 ) );
}
它有效,但有一点问题。我使用EPIC插件在Eclipse中编码,并且如果你在BEGIN中有某些内容,那么阻止导致编译器过早中止,它不会向EPIC报告语法错误,这样我就可以在模块中放松Perl语法检查。
所以,使用 FindBin
(在脚本中)我不必在 BEGIN {} catdir
) / code> block,以下代码的语法检查正确进行。此外,我不想更改任何环境变量(例如 PERL5LIB
),这样我就可以在同事的机器上使用脚本而无需任何额外的准备。
It works but with a little problem. I code in Eclipse with EPIC plugin, and "if you have something in a BEGIN block that causes the compiler to abort prematurely, it won't report syntax errors to EPIC", so that way I loose Perl syntax check in modules.
So, with FindBin
(in scripts) I don't have to use any functions (like catdir
) in BEGIN{}
block, and the syntax check of the following code goes on correctly. Besides, I'd like not to change any environment variables (like PERL5LIB
), so that I could use the scripts on my colleagues' machines without any additional preparations.
在其他模块中使用自定义Perl模块的正确方法是什么,而不是同时干扰EPIC语法检查?或者我甚至应该以其他方式包含模块?
What's the proper way of using custom Perl modules inside of other modules, and not interfering with EPIC syntax check at the same time? Or maybe I even should include modules in completely other way?
我强烈不同意修改 @INC
在模块中。它会引起各种头痛。让脚本(甚至通过 PERL5LIB
环境变量调用进程)正确设置 @INC
。
I strongly disagree with modifying @INC
in modules. It causes all kinds of headaches. Let the script (or even the calling process via the PERL5LIB
environment variable) setup @INC
correctly.
script.pl
:
use FindBin qw( $RealBin );
use lib
"$RealBin/../ModulesFolder1",
"$RealBin/../ModulesFolder2";
use ModuleInFolder1;
ModuleInFolder1.pm
:
use ModuleInFolder2; # Works fine.
至于EPIC,请执行以下操作:
As for EPIC, do the following:
- 右键单击该项目。
- 属性
- Perl包含路径
-
$ {project_loc} / ModulesFolder1
,添加到列表 -
$ {project_loc} / ModulesFolder2
,添加到列表
- Right-click on the project.
- Properties
- Perl Include Path
-
${project_loc}/ModulesFolder1
, Add to list -
${project_loc}/ModulesFolder2
, Add to list
(我的意思是14 chars $ {project_loc}
。这对EPIC来说意味着什么。即使你移动项目它也会继续工作。)
(I literally mean the 14 chars ${project_loc}
. That means something to EPIC. It will continue to work even if you move the project.)
PS— $ RealBin
优于 $ Bin
,因为它允许您对脚本使用符号链接。
PS — $RealBin
is better than $Bin
because it allows you to use a symlink to your script.
PS— __ FILE __
比 $ INC {'ThisModule.pm'}
更合适。
PS — __FILE__
is more appropriate than $INC{'ThisModule.pm'}
.