Perl + Unicode:“宽字符串”错误

发布于 2025-01-05 18:31:39 字数 1236 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2025-01-12 18:31:39

Perl 调试器有自己的输出句柄,与 STDOUT 不同(尽管它最终可能会到达与 STDOUT 相同的位置)。您还需要在脚本开头附近执行类似的操作:

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

The Perl debugger has its own output handle that is distinct from STDOUT (although it may ultimately go to the same place as STDOUT). You'll also want to do something like this near the beginning of your script:

binmode $DB::OUT, ':utf8' if $DB::OUT;
痴意少年 2025-01-12 18:31:39

我怀疑问题出在您没有向我们展示的代码的某些部分。我的怀疑基于以下事实:

  1. 您引用的错误消息显示at (eval 155)。您的代码中没有 eval

  2. 当我运行上面的代码时,即使输入包含 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:

  1. The error message you quote says at (eval 155). There are no evals in your code.

  2. 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 the binmode 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文