Perl 为什么一个模块自己编译,但从其他地方使用时失败?
我有一个Perl模块,它本身似乎编译得很好,但在包含它时会导致其他程序编译失败:Perl 为什么一个模块自己编译,但从其他地方使用时失败?,perl,perl-module,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
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报告状态的前几行是
...
3 use Rebat;
4 use Rebat::Store;
5 use strict;
...
编辑(面向后代):发生这种情况的另一个原因,也许是最常见的原因,是您使用的模块之间存在循环依赖关系
在Rebat/Store.pm中查找线索。您的日志显示重新加载的尝试已中止。可能Rebat
已经导入了Rebat::Store
,并且Rebat::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;
如果删除M1.pl
中的use M1::M2
行,代码将编译(并打印42)。在您的情况下,您可能不需要在程序中显式地使用Rebat::Store
。:
事实上,问题是由于Rebat.pm和Rebat/Store.pm之间新引入了循环依赖关系
$ perl m1.pl
Attempt to reload M1::M2 aborted.
BEGIN failed--compilation aborted at m1.pl line 3.
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.