为什么模块可以自行编译,但从其他地方使用时会失败?
我有一个 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;
...
I have a Perl module that appears to compile fine by itself, but is causing other programs to fail compilation when it is included:
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.
The first few lines of rebat-report-status
are
...
3 use Rebat;
4 use Rebat::Store;
5 use strict;
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑(供后代):发生这种情况的另一个原因(也许是最常见的原因)是您正在使用的模块之间存在循环依赖关系。
在
Rebat/Store.pm
中查找线索。您的日志显示尝试重新加载已中止。也许Rebat
已经导入了Rebat::Store
,并且Rebat::Store
进行了一些包范围检查,以防止加载两次。这段代码演示了我所说的那种情况:
如果您只是删除
m1.pl
中的use M1::M2
行,代码将编译(并打印 42)。就您而言,您可能不需要在程序中显式使用Rebat::Store
。Edit (for posterity): Another reason for this to occur, and perhaps the most common reason, is that there is a circular dependency among the modules you are using.
Look in
Rebat/Store.pm
for clues. Your log says attempt to reload was aborted. MaybeRebat
already importsRebat::Store
, andRebat::Store
has some package-scope check against being loaded twice.This code demonstrates the kind of situation I mean:
The code will compile (and print 42) if you just remove the
use M1::M2
line inm1.pl
. In your case, you might not need to explicitlyuse Rebat::Store
in your program.perldoc perldiag:
perldoc perldiag: