我在我的脚本中使用自定义模块,并且必须将它们存储在Perl lib目录之外。 所以在Perl脚本( *.pl )中我使用以下块将它们包含在@INC :
BEGIN { use FindBin qw($Bin); push @INC, "$Bin/../ModulesFolder1"; push @INC, "$Bin/../ModulesFolder2"; }但我还必须在我的其他Perl模块( *.pm )中使用模块,并且我理解FindBin仅适用于脚本。 所以我将该块更改为:
BEGIN { push @INC, catdir( dirname( $INC{'ThisModule.pm'} ), qw( .. ModulesFolder1 ) ); push @INC, catdir( dirname( $INC{'ThisModule.pm'} ), qw( .. ModulesFolder2 ) ); }它有效,但有一点问题。 我在Eclipse中使用EPIC插件进行编码,并且“如果你在BEGIN块中有某些东西会导致编译器过早中止,它就不会向EPIC报告语法错误” ,这样我就可以放松Perl语法检查模块。 因此,使用FindBin (在脚本中)我不必在BEGIN{}块中使用任何函数(如catdir ),并且以下代码的语法检查正确进行。 此外,我不想更改任何环境变量(如PERL5LIB ),以便我可以在同事的机器上使用脚本而无需任何额外的准备工作。
在其他模块中使用自定义Perl模块的正确方法是什么,而不是同时干扰EPIC语法检查? 或者我甚至应该以其他方式包含模块?
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"; }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 ) ); }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.
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 。
script.pl :
use FindBin qw( $RealBin ); use lib "$RealBin/../ModulesFolder1", "$RealBin/../ModulesFolder2"; use ModuleInFolder1;ModuleInFolder1.pm :
use ModuleInFolder2; # Works fine.至于EPIC,请执行以下操作:
右键单击该项目。 属性 Perl包含路径 ${project_loc}/ModulesFolder1 ,添加到列表 ${project_loc}/ModulesFolder2 ,添加到列表(我的意思是14个字符${project_loc} 。这对EPIC来说意味着什么。即使你移动项目它也会继续工作。)
PS - $RealBin优于$Bin因为它允许您使用符号链接到您的脚本。
PS - __FILE__比$INC{'ThisModule.pm'}更合适。
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.As for EPIC, do the following:
Right-click on the project. Properties Perl Include Path ${project_loc}/ModulesFolder1, Add to list ${project_loc}/ModulesFolder2, Add to list(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 is better than $Bin because it allows you to use a symlink to your script.
PS — __FILE__ is more appropriate than $INC{'ThisModule.pm'}.
更多推荐
发布评论