如何让 perl -c 抛出未定义或未声明的函数错误?

发布于 2024-12-20 21:40:31 字数 449 浏览 0 评论 0原文

由于具有 C++ 背景,我虔诚地使用 Perl 的 use strictuse 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请你别敷衍 2024-12-27 21:40:31

试试这个:

perl -MO=Lint -cw /path/to/script.pl

这使用 B::Lint 模块

Try this:

perl -MO=Lint -cw /path/to/script.pl

This uses the B::Lint module.

我乃一代侩神 2024-12-27 21:40:31

模块 Sub::StrictDecl 执行您正在寻找的操作,并且具有词法范围。

该模块提供了对子例程是否存在的可选检查
编译时间。此检查可检测输入错误的子例程名称和
程序员忘记导入的子例程。传统 Perl
直到运行时才检测到这些错误,因此很容易出错
潜伏在很少执行或未经测试的代码中。

具体来说,在启用检查的情况下,对特定的任何引用
(编译时常数)检查基于包的子例程名称。如果
指定的子例程从未被声明过,那么错误是
在编译时发出信号。这并不要求子程序
完全定义:前向声明,例如“sub foo;”足以
抑制错误。导入的子例程符合声明的条件。
检查的引用不仅包括子程序调用,还包括
纯引用,例如“\&foo”。

此检查由词法范围的编译指示控制。这是
因此仅适用于明确需要检查的代码,并且
如有必要,可以在本地禁用检查。检查
对于进行特殊安排的代码可能需要关闭
例如,在运行时放置子例程。

The module Sub::StrictDecl does what you are looking for, and with lexical scope.

This module provides optional checking of subroutine existence at
compile time. This checking detects mistyped subroutine names and
subroutines that the programmer forgot to import. Traditionally Perl
does not detect these errors until runtime, so it is easy for errors
to lurk in rarely-executed or untested code.

Specifically, where checking is enabled, any reference to a specific
(compile-time-constant) package-based subroutine name is examined. If
the named subroutine has never been declared then an error is
signalled at compile time. This does not require that the subroutine
be fully defined: a forward declaration such as "sub foo;" suffices to
suppress the error. Imported subroutines qualify as declared.
References that are checked include not only subroutine calls but also
pure referencing such as "\&foo".

This checking is controlled by a lexically-scoped pragma. It is
therefore applied only to code that explicitly wants the checking, and
it is possible to locally disable checking if necessary. Checking
might need to be turned off for code that makes special arrangements
to put a subroutine in place at runtime, for example.

奢望 2024-12-27 21:40:31

看到 ikegami 的回答提醒我 perlcritic 可以识别未声明的子目录,但您需要安装 Perl::Critic::StricterSubs 策略,它不是核心 Perl::Critic 发行版的一部分。

perlcritic -4 mycode.pl

子例程“foobar”既没有声明也没有显式导入
第 10 行,第 1 列。这可能是一个主要错误。 (严重性:4)

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 core Perl::Critic distribution.

perlcritic -4 mycode.pl

Subroutine "foobar" is neither declared nor explicitly imported at
line 10, column 1. This might be a major bug. (Severity: 4)

痞味浪人 2024-12-27 21:40:31

Perl 在编译时不可能知道一旦到达 sub 调用就不会再有 sub 来调用,因此 -c 不可能告诉你这一点。

perlcritic 是一个旨在扫描 Perl 代码并猜测的工具 可能出现这样的问题。 Perl::Critic::StricterSubs perlcritic 规则检查此问题。

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::StricterSubs perlcritic rule checks for this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文