perl - 不正确的Perl语法会产生不正确的错误消息

标签 perl syntax-error

以下(经过简化的代码)有一个明显的错误。

#!/usr/bin/env perl

use strict;
use warnings FATAL=>"all";

my $err_phyle;
my $verb;

sub do_measure
{
  print($error_phyle "xxx");

}

sub dye
{
}

dye "invalid verb \"$verb\"";

您希望错误消息是:
Global symbol "$error_phyle" requires explicit package name at example.pl line 11.
example.pl had compilation errors.

...但实际上是...
String found where operator expected at example.pl line 19, near "dye "invalid verb \"$verb\"""

(请注意,第二条错误消息未添加“example.pl发生编译错误。”。)

在实际程序中,错误消息指向与实际错误相距数百行的代码。指向错误行的错误消息不正确,可能会导致试图找出语法错误的原因很多。

我可以在上面的程序中通过用括号括住对函数dye的调用参数来“修复”此问题,我想我将遍历该程序并执行此操作。但这是Perl中的bug,还是(很可能是这样)我是否缺少有关Perl工作方式的信息?

最佳答案

您可以使用diagnostics编译指示来获取有关错误的更多信息。

The diagnostics Pragma

This module extends the terse diagnostics normally emitted by both the perl compiler and the perl interpreter (from running perl with a -w switch or use warnings ), augmenting them with the more explicative and endearing descriptions found in perldiag. Like the other pragmata, it affects the compilation phase of your program rather than merely the execution phase. To use in your program as a pragma, merely invoke

use diagnostics;  

at the start (or near the start) of your program. (Note that this does enable perl's -w flag.) Your whole compilation will then be subject(ed :-) to the enhanced diagnostics. These still go out STDERR.



根据您的评论:这也可以使旧版本的perl显示两个错误消息,从而可以节省一些调试工作来查找实际错误的位置。

关于perl - 不正确的Perl语法会产生不正确的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43447894/

相关文章:

windows - 如何在 Windows 上使用 x86 perl 系统启动 x64 shell

perl如何引用散列本身

date - Access 2010 VBA语法错误 '3075'

syntax-error - 语法错误,基本中缺少符号

php - php方法意外的t_string

javascript - 在Google表格中按状态自动发送电子邮件

xml - perl LibXML 编写XML文档

perl - fetchall_arrayref 中的 foreach 值

perl - 如何在同一个 nginx 服务器上运行多个 perl Dancer2 应用程序

php - SQL 语法错误或服务器错误?