以下(经过简化的代码)有一个明显的错误。
#!/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
PragmaThis 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 invokeuse 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/