为什么模块本身编译但在从其他地方使用时会失败?

时间:2010-02-04 18:42:24

标签: perl perl-module

我有一个Perl模块似乎可以自行编译,但是当包含它时,会导致其他程序编译失败:

me@host:~/code $ perl -c -Imodules modules/Rebat/Store.pm
modules/Rebat/Store.pm syntax OK
me@host:~/code $ perl -c -Imodules bin/rebat-report-status
Attempt to reload Rebat/Store.pm aborted
Compilation failed in require at bin/rebat-report-status line 4.
BEGIN failed--compilation aborted at bin/rebat-report-status line 4.

rebat-report-status的前几行是

...
3 use Rebat;
4 use Rebat::Store;
5 use strict;
...

2 个答案:

答案 0 :(得分:9)

编辑(对于子孙后代):出现这种情况的另一个原因,也许是最常见的原因,就是您正在使用的模块之间存在循环依赖关系。


查看Rebat/Store.pm的线索。您的日志表示尝试重新加载已中止。可能Rebat已经导入Rebat::StoreRebat::Store有一些包装范围检查,反对加载两次。

此代码演示了我的意思:

# m1.pl:
use M1;
use M1::M2;
M1::M2::x();

# M1.pm 
package M1;
use M1::M2;
1;

# M1/M2.pm
package M1::M2;
our $imported = 0;
sub import {
    die "Attempt to reload M1::M2 aborted.\n" if $imported++;
}
sub x { print "42\n" }
1;

$ perl m1.pl
Attempt to reload M1::M2 aborted.
BEGIN failed--compilation aborted at m1.pl line 3.

如果您只删除use M1::M2中的m1.pl行,代码将会编译(并打印42)。在您的情况下,您可能不需要在程序中明确use Rebat::Store

答案 1 :(得分:4)

perldoc perldiag

 Attempt to reload %s aborted.
           (F) You tried to load a file with "use" or "require" that failed to
           compile once already.  Perl will not try to compile this file again
           unless you delete its entry from %INC.  See "require" in perlfunc
           and "%INC" in perlvar.