我在将 Unicode 字符串打印到 Windows 控制台时遇到了一个奇怪的问题*。
考虑这段文字:
אני רוצה לישון
Intermediary
היא רוצה לישון
אתם, הם
Bye
Hello, world!
test
假设它在一个名为“file.txt”的文件中。
当我去*:“type file.txt”时,它打印得很好。但是当它从 Perl 程序中打印出来时,像这样:
use strict;
use warnings;
use Encode;
use 5.014;
use utf8;
use autodie;
use warnings qw< FATAL utf8 >;
use open qw< :std :utf8 >;
use feature qw< unicode_strings >;
use warnings 'all';
binmode STDOUT, ':utf8'; # output should be in UTF-8
my $word;
my @array = ( 'אני רוצה לישון', 'Intermediary',
'היא רוצה לישון', 'אתם, הם', 'Bye','Hello, world!', 'test');
foreach $word(@array) {
say $word;
}
Unicode 行(在这种情况下为希伯来语)每次都会再次出现,部分中断,如下所示:
E:\My Documents\Technical\Perl>perl "hello unicode.pl"
אני רוצה לישון
לישון
�ן
Intermediary
היא רוצה לישון
לישון
�ן
אתם, הם
�ם
Bye
Hello, world!
test
(我将所有内容都保存在 UTF-8 中)。
这很奇怪。有什么建议么?
(这不是“Console2”问题*——同样的问题出现在“常规”Windows 控制台上,只是在那里您看不到希伯来语字形)。
* 使用“Console”(也称为“Console2”) - 这是一个不错的小实用程序,可以在 Windows 控制台上使用 Unicode - 例如,请参见: http ://www.hanselman.com/blog/Console2ABetterWindowsCommandPrompt.aspx
** 注意:在控制台,你当然要说:
chcp 65001