Perl + Unicode : "Wide Strings" error

标签 perl unicode utf

我在 Windows 7 上运行 Active Perl 5.14。
我正在尝试编写一个程序,该程序将读入转换表,然后处理文件并用其他模式替换某些模式 - 以上所有内容均采用 Unicode (UTF-8)。这是程序的开头:

#!/usr/local/bin/perl
# Load a conversion table from CONVTABLE to %ConvTable.
# Then find matches in a file and convert them.
use strict;
use warnings;
use Encode;
use 5.014;
use utf8;
use autodie; 
use warnings    qw< FATAL  utf8     >;
use open        qw< :std  :utf8     >;
use charnames   qw< :full >;
use feature     qw< unicode_strings >;

my ($i,$j,$InputFile, $OutputFile,$word,$from,$to,$linetoprint);
my (@line, @lineout); 
my %ConvTable;    # Conversion hash
print 'Conversion table: opening file: E:\My Documents\Perl\Conversion table.txt'."\n";
my $sta= open (CONVTABLE, "<:encoding(utf8)", 'E:\My Documents\Perl\Conversion table.txt');
binmode STDOUT, ':utf8';    # output should be in UTF-8
# Load conversion hash
while (<CONVTABLE>) {
    chomp;
    print "$_\n"; # etc ...
# etc ...

事实证明,在这一点上,它说:
wide character in print at (eval 155)E:/Active Perl/lib/Perl5DB.pl:640]line 2, <CONVTABLE> line 1, etc...

这是为什么?我想我已经完成并实现了正确处理 Unicode 字符串、解码和编码为 UTF-8 的所有必要规定?
以及如何修复它?

TIA

海伦

最佳答案

Perl 调试器有自己的输出句柄,不同于 STDOUT (尽管它最终可能会与 STDOUT 到达同一个地方)。您还需要在脚本开头附近执行以下操作:

binmode $DB::OUT, ':utf8' if $DB::OUT;

关于Perl + Unicode : "Wide Strings" error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9299941/

相关文章:

javascript - 在 Javascript (Ajax) 中检索二进制数据

php - 如何在 PHP 中验证 utf 序列?

git - 在 Git::Repository 中使用密码

java - 韩文未出现在Apache Netbeans中

ruby-on-rails - Ruby on Rails 3 表单中的 _snowman 参数有什么用?

objective-c - 在  应用程序中使用 unicode 符号或形状是不好的做法吗?

windows - PAR 打包程序可执行文件大小

perl - 如何减少常量中的重复?

perl - 在 Perl 中使用一个哈希引用而不是两个

c# - 需要帮助理解 UTF 编码