如果在多行替换中死亡,Perl会报告错误的行号.这是预期还是错误?

Ren*_*ger 8 windows perl

当我运行以下代码时,脚本会死掉expected three at c:\temp\die.pl line 13..

use warnings;
use strict;

my $text = 'one two Three';

$text =~ s{

  (\w+)  # Find a word
  \s+
  (\w+)  # Find another word
  \s+
  (\w+)  # Find third word

}{

   # Insert a few lines on purpose
   # to make the point.

   die 'expected one'   unless $1 eq 'one';
   die 'expected two'   unless $2 eq 'two';
   die 'expected three' unless $3 eq 'three';

}ex;
Run Code Online (Sandbox Code Playgroud)

我有点惊讶,因为导致死亡的实际线是第21行.

似乎解释器在计算(?)死亡线时不计算替换的模式部分.第6行将是$text =~ s{代码,直到}{"跳过",使第7行成为下面的空行,第8行,# Insert a few lines on purpose依此类推,直到它die 'expected three' unless $3 eq 'three';计算出13行.

这种行为有望吗?

有没有办法让perl打印真正的死亡线?

perl -v 以..开始 This is perl 5, version 18, subversion 1 (v5.18.1) built for MSWin32-x86-multi-thread-64int

用鲤鱼代替死

有人建议我用carp而不是die.

它现在死了(或鲤鱼),只有稍微不同expected three at c:\temp\die.pl line 14,我将其归属于额外的第三行use Carp;.

小智 4

快速的调查表明:

v5.10.1死于第 13 行。
v5.18.4死于第 13 行。
v5.20.2死于第 21 行。
v5.22.0死于第 21 行。

然后在检查5.20 的 perldelta后,我发现:

现在可以正确报告多行引用式运算符内的行号。[perl#3643]

因此,获得正确行号的唯一方法似乎是升级到更高版本的 perl。