Perl + Unicode:“宽字符串”错误
我在 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 的所有必要规定? 以及如何修复它?
蒂亚·
海伦
I am running Active Perl 5.14 on Windows 7.
I am trying to write a program that will read-in a conversion table, then work on a file and replace certain patterns by other patterns - all of the above in Unicode (UTF-8). Here is the beginning of the program:
#!/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 ...
It turns out that at this point, it says:
wide character in print at (eval 155)E:/Active Perl/lib/Perl5DB.pl:640]line 2, <CONVTABLE> line 1, etc...
Why is that? I think I've gone through and implemented all the necessary prescriptions for correct handling of Unicode strings, decoding and encoding into UTF-8?
And how to fix it?
TIA
Helen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Perl 调试器有自己的输出句柄,与
STDOUT
不同(尽管它最终可能会到达与STDOUT
相同的位置)。您还需要在脚本开头附近执行类似的操作:The Perl debugger has its own output handle that is distinct from
STDOUT
(although it may ultimately go to the same place asSTDOUT
). You'll also want to do something like this near the beginning of your script:我怀疑问题出在您没有向我们展示的代码的某些部分。我的怀疑基于以下事实:
您引用的错误消息显示
at (eval 155)
。您的代码中没有eval
。当我运行上面的代码时,即使输入包含 Unicode 字符,不会产生“宽字符”警告。我可以让它生成一个的唯一方法是注释掉
use open
行和binmode STDOUT
行。诚然,我的测试环境与你的并不完全相同:我在 Linux 上,我的 Perl 只有 v5.10.1,这意味着我必须降低版本要求并关闭
unicode_strings
功能(并不是您实际使用它)。不过,我非常怀疑问题不在您发布的代码中。I suspect that the problem is in some part of the code that you haven't shown us. I base this suspicion on the following facts:
The error message you quote says
at (eval 155)
. There are noeval
s in your code.The code you have shown us above does not produce a "wide character" warning when I run it, even if the input contains Unicode characters. The only way I can make it produce one is to comment out both the
use open
line and thebinmode STDOUT
line.Admittedly, my testing environment is not exactly identical to yours: I'm on Linux, and my Perl is only v5.10.1, meaning that I had to lower the version requirement and turn off the
unicode_strings
feature (not that you're actually using it). Still, I very much suspect that the problem is not in the code you've posted.