在其他Perl模块中使用Perl自定义模块的正确方法 [英] A proper way of using Perl custom modules inside of other Perl modules

查看:847
本文介绍了在其他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:


  1. 右键单击该项目。

  2. 属性

  3. Perl包含路径

  4. $ {project_loc} / ModulesFolder1 ,添加到列表

  5. $ {project_loc} / ModulesFolder2 ,添加到列表

  1. Right-click on the project.
  2. Properties
  3. Perl Include Path
  4. ${project_loc}/ModulesFolder1, Add to list
  5. ${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'}.

这篇关于在其他Perl模块中使用Perl自定义模块的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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