perl – 如何抽出未定义的子程序?

前端之家收集整理的这篇文章主要介绍了perl – 如何抽出未定义的子程序?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想扫描一个代码库来识别当前不可达的未定义子例程的所有实例.

举个例子:

use strict;
use warnings;

my $flag = 0;
if ( $flag ) {
  undefined_sub();
}

意见

>当$flag评估为true时,将发出以下警告:

Undefined subroutine &main::undefined_sub called at - line 6

我不想依赖在运行时发出的警告来识别未定义的子例程
>严格的和警告的pragmas在这里没有帮助.使用严格的’subs’没有任何效果.
>即使以下代码片断也是沉默的

$perl -Mstrict -we 'exit 0; undefined_sub()'

解决方法

也许从 Perl::Critic Subroutines::ProhibitCallsToUndeclaredSubs政策可以帮助

This Policy checks that every unqualified subroutine call has a matching subroutine declaration in the current file,or that it explicitly appears in the import list for one of the included modules.

这个“政策”是Perl::Critic::StricterSubs的一部分,需要安装.那里还有一些政策.这被认为是严重性4违反,所以你可以做

perlcritic -4 script.pl

并解析既不声明也不明确导入或使用的输出

perlcritic -4 --single-policy ProhibitCallsToUndeclaredSubs script.pl

一些合法用途仍然被标记,因为它需要明确导入所有的子.

这是一个静态分析仪,我认为应该符合您的目的.

原文链接:https://www.f2er.com/Perl/172789.html

猜你在找的Perl相关文章