如何让 perl -c 抛出未定义或未声明的函数错误?
由于具有 C++ 背景,我虔诚地使用 Perl 的 use strict
和 use warnings
功能:
#!/usr/bin/perl -w
use strict;
use warnings;
$foo = 1; #Throws "$foo" requires explicit package name error
foobar( 1 );
use strict
构造对于捕获错误非常有帮助当你输入错误的变量名时。是否有等效的构造来捕获输入错误的函数名称?在上面的示例中,如果有类似 perl -c
的东西能够捕获没有 foobar 函数可供调用的事实,那就太好了。当然,运行脚本会抛出未定义的子例程错误,但我想尽快捕获它。
Coming from a C++ background, I religiously use the use strict
and use warnings
features of Perl:
#!/usr/bin/perl -w
use strict;
use warnings;
$foo = 1; #Throws "$foo" requires explicit package name error
foobar( 1 );
The use strict
construct is immensely helpful to catch errors when you mistype a variable name. Is there an equivalent construct to catch mistyped function names? In the above example, it would be great if there was something like perl -c
that caught the fact that there is no foobar function available to call. Of course running the script throws an Undefined subroutine error, but I would like to catch it sooner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
这使用 B::Lint 模块。
Try this:
This uses the B::Lint module.
模块 Sub::StrictDecl 执行您正在寻找的操作,并且具有词法范围。
The module Sub::StrictDecl does what you are looking for, and with lexical scope.
看到
ikegami
的回答提醒我 perlcritic 可以识别未声明的子目录,但您需要安装 Perl::Critic::StricterSubs 策略,它不是核心Perl::Critic
发行版的一部分。Seeing
ikegami
's Answer reminded me that perlcritic can identify undeclared subs, but you need to install the Perl::Critic::StricterSubs policy, which is not part of the corePerl::Critic
distribution.Perl 在编译时不可能知道一旦到达 sub 调用就不会再有 sub 来调用,因此
-c
不可能告诉你这一点。perlcritic
是一个旨在扫描 Perl 代码并猜测的工具 可能出现这样的问题。 Perl::Critic::StricterSubsperlcritic
规则检查此问题。Perl cannot possibly know at compile time that there won't be a sub to call once the sub call is reached, so
-c
cannot possibly tell you that.perlcritic
is a tool designed to scan Perl code and guess at possible problems like this one. The Perl::Critic::StricterSubsperlcritic
rule checks for this problem.