Perl-重新定义子程序 [英] Perl - Subroutine redefined

查看:213
本文介绍了Perl-重新定义子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过这个问题,或者经过搜索发现其他人在问-为什么我得到警告"在../lib/Common.pm第x 行重新定义了子例程mySub"?并且总是得到答案您在同一代码中两次声明了该子代码.我创建了这个测试包:

I have asked this question before or searched and seen others ask - why am I getting the warning "Subroutine mySub redefined at ../lib/Common.pm line x"? and you always get the answer you declared the sub twice in the same code. I created this test package:

整个文件---------------

ENTIRE FILE ---------------

package MyCommonPkg;

use strict;

sub thisSubroutineIsNotDefinedAnywhereElse{
}

1;

整个文件---------------

ENTIRE FILE ---------------

,我从perl脚本中使用了此软件包,该脚本使用了其他软件包,也使用了此软件包,并且得到警告:

and I USE this package from a perl script, which uses other packages, that use this package also, and I get the warning:

子例程ThisSubroutineIsNotDefinedAnywhereElse在../lib/MyCommonPkg.pm第19行重新定义.

我保证我不会在其他任何地方声明此子对象.那么这是由循环引用引起的吗?我该如何跟踪并修复此警告的原因?

I promise I did not declare this sub anywhere else. So is this caused by a circular reference? How can I go about tracking the cause of this warning down and fixing?

推荐答案

您是否有依赖项循环?如果Perl开始编译您的脚本并遇到这样的一行:

Do you have a dependency loop? If Perl starts compiling your script and encounters a line like this:

use PackageA;

Perl暂停脚本的编译;找到PackageA.pm并开始对其进行编译.如果遇到这样的一行:

Perl pauses the compilation of your script; locates PackageA.pm and starts compiling it. If it encounters a line like this:

use PackageB;

Perl暂停PackageA的编译;找到PackageB.pm并开始对其进行编译.通常,这将成功完成,而Perl将返回完成对PackageA的编译,当成功完成后,它将返回到编译脚本,而当成功完成时,它将开始执行已编译的操作码.

Perl pauses the compilation of PackageA; locates PackageB.pm and starts compiling it. Normally, that would complete successfully, and Perl would go back to complete compiling PackageA and when that completes successfully it would go back to compiling your script and when that completes successfully it would start to execute the compiled opcodes.

但是,如果PackageB.pm包含以下行:

However, if PackageB.pm contains this line:

use PackageA;

您可能希望它不会做任何事情,因为Perl已经处理了PackageA.pm,但是问题在于它还没有完成.因此,Perl将暂停PackageB的编译,并从头开始重新编译PackageA.pm.这可能会触发有关重新定义PackageA中的子例程的消息.

You might expect it would do nothing since Perl has already processed PackageA.pm but the problem is that it hasn't finished yet. So Perl will pause the compilation of PackageB and start compiling PackageA.pm again from the beginning. That could trigger the message you're seeing about subroutines in PackageA being redefined.

作为一般规则,两个程序包不应相互依赖.但是有时有时很难找到该循环,因为它是由第三个程序包引起的.

As a general rule, two packages should not both depend on each other. Sometimes however the loop is harder to locate because it is caused by a third package.

这篇关于Perl-重新定义子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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