【发布时间】:2012-03-07 04:21:47
【问题描述】:
我在 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
海伦
【问题讨论】:
-
你的代码没问题。如果你在调试器之外运行它会发生什么?
-
顺便说一下,
binmode STDOUT, ':utf8';是多余的,use open qw< :std :utf8 >;也是如此。 -
顺便说一下,
use feature qw< unicode_strings >;(对代码没有影响)是多余的,use 5.014;也是如此。