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

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

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

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...