【问题标题】:Unicode::GCString error-message: "new: Unicode string must be given"Unicode::GCString 错误消息:“新:必须给出 Unicode 字符串”
【发布时间】:2012-02-22 07:44:24
【问题描述】:

这里可能有什么问题:我收到错误消息
new: Unicode string must be given at ...
换行
$gvalue = Unicode::GCString->new( $value );

use Unicode::GCString;

# ....
# ....

my $width = 0;
my $gvalue;

if ( $value ) {
    $gvalue = Unicode::GCString->new( $value );
    $width = $gvalue->columns();
}

# ....
# new: Unicode string must be given. at ...

$values 出现:

for my $i ( 0 .. $#$ref ) {
    for my $j ( 0 .. $#{$ref->[$i]} ) {
        my $value = $ref->[$i][$j] // '';
        # ...

到目前为止,在测试 $ref 时,它是硬编码在脚本中的,但它应该成为一个模块,而 $ref 应该在例程中转移。

【问题讨论】:

  • 我试图在 Unicode::GCString 模块中查找错误消息,但错误消息在二进制文件中。
  • 好的,ref 是如何获取它的值的呢?我实际上是在尝试追溯原始数据,看看您是否在其他地方有问题。这就是为什么我总是建议人们提供完整的示例程序和说明问题的输入。

标签: perl unicode


【解决方案1】:

Unicode::GCString 存在 Unicode 错误(将含义分配给 UTF8 标志):

 $ perl -MUnicode::GCString -E'
    $_=chr(0xE9);
    utf8::downgrade($_);
    Unicode::GCString->new($_);
    say "ok";
 '
 new: Unicode string must be given. at -e line 4.

 $ perl -MUnicode::GCString -E'
    $_=chr(0xE9);
    utf8::upgrade($_);
    Unicode::GCString->new($_);
    say "ok";
 '
 ok

它需要使用 UTF8=1 内部存储格式存储的字符串。您可以使用utf8::upgrade 将字符串强制转换为正确的格式,但这也可能表明您忘记解码字符串。

【讨论】:

  • 我相信这在当前版本中已修复。
  • @tchirst,我有最新的(来自 NEZUMI/Unicode-LineBreak-2012.02.tar.gz 的 Unicode::UCString 2011.03)。还尝试了最新的开发版本 (NEZUMI/Unicode-LineBreak-2012.001_29.tar.gz)。
【解决方案2】:

我忘了设置use utf8(我必须明确地字符串化$gstring->as_string)。

【讨论】:

  • 对于 utf8 文字和标识符,始终以 use utf8 开头。
猜你喜欢
  • 1970-01-01
  • 2012-03-07
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多