为什么这个带有2字节unicode char的正则表达式会对匹配中的左值发出“未初始化”警告?

时间:2012-09-10 20:09:24

标签: regex perl unicode

以下代码:

#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use 5.012; # implicitly turn on feature unicode_strings
my $test = "some string";
$test =~ m/.+\x{2013}/x;

收率:

  

在test.pl第9行的模式匹配$test中使用未初始化的值(m//)

这似乎发生在\x{}内的任何2字节字符。以下正则表达式正常工作:

/a+\x{2013}/
/.*\x{2013}/
/.+\x{20}/

此外,use bytes错误消失了,但使用该编译指示是discouraged。这是怎么回事?

2 个答案:

答案 0 :(得分:5)

这是一个错误,现在已通过提交7e0d5ad7c9cdb21b681e611b888acd41d34c4d05修复了blead c72077c4fff72b66cdde1621c62fb4fd383ce093。此修复程序应在5.17.5中提供

答案 1 :(得分:3)

你应该问这个问题是很奇怪的。我看起来与昨天刚刚报道的一个错误相关

https://rt.perl.org/rt3/Ticket/Display.html?id=114808

此代码也会产生"Use of uninitialized value $_ in split ..."警告,并导致split意外返回空列表:

use warnings;
binmode *STDOUT, ":encoding(UTF-8)";
my $pattern = "\x{abc}\x{def}ghi";
for ( "\x{444}", "norm\x{a0}l", "\x{445}", "ab\x{ccc}de\x{fff}gh" ) {
  print "--------------------\ntext is $_, pattern is /$pattern/\n";

  # expect  split  to return  ($_) , but when $pattern and $_ both
  # have wide chars, it returns  ()
  print 'split output is [', split /$pattern/, $_;

  print "]\n";
}