问题描述
我想扫描代码库以识别当前无法访问的未定义子例程的所有实例.
I want to scan a code base to identify all instances of undefined subroutines that are not presently reachable.
举个例子:
use strict;
use warnings;
my $flag = 0;
if ( $flag ) {
undefined_sub();
}
观察
当
$flag
的计算结果为 true 时,会发出以下警告:Observations
When
$flag
evaluates to true, the following warning is emitted:Undefined subroutine &main::undefined_sub called at - line 6
我不想依赖运行时发出的警告来识别未定义的子例程
I don't want to rely on warnings issued at run-time to identify undefined subroutines
strict
和warnings
编译指示在这里没有帮助.use strict 'subs'
没有效果.The
strict
andwarnings
pragmas don't help here.use strict 'subs'
has no effect.即使是下面的代码片段也是无声的
Even the following code snippet is silent
$ perl -Mstrict -we 'exit 0; undefined_sub()'
推荐答案
也许 Subroutines::ProhibitCallsToUndeclaredSubs 政策来自 Perl::Critic 可以提供帮助
Perhaps Subroutines::ProhibitCallsToUndeclaredSubs policy from Perl::Critic can help
此策略检查每个未限定的子例程调用在当前文件中是否具有匹配的子例程声明,或者它是否明确出现在包含模块之一的导入列表中.
这个政策"是Perl::Critic::StricterSubs,需要安装.那里还有一些政策.这被视为严重性 4 级违规,因此您可以这样做
This "policy" is a part of Perl::Critic::StricterSubs, which needs to be installed. There are a few more policies there. This is considered a severity 4 violation, so you can do
perlcritic -4 script.pl
并解析
既未声明也未显式导入
的输出,或使用perlcritic -4 --single-policy ProhibitCallsToUndeclaredSubs script.pl
某些合法用途仍被标记,因为它要求明确导入所有子项.
Some legitimate uses are still flagged, since it requires all subs to be imported explicitly.
这是一个静态分析器,我认为它应该适合您的目的.
This is a static analyzer, which I think should fit your purpose.
这篇关于如何消除未定义的子程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!