Perl:将Unicode字符串打印到Windows控制台

时间:2012-02-21 01:02:33

标签: perl unicode utf-8 console unicode-string

我在向Windows控制台打印Unicode字符串时遇到一个奇怪的问题*。

考虑这个文字:

אני רוצה לישון

Intermediary

היא רוצה לישון
אתם, הם
Bye
Hello, world!
test

假设它位于名为“file.txt”的文件中。

当我去*:“输入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

4 个答案:

答案 0 :(得分:4)

您是否尝试过perlmonk的解决方案?

它也使用:unix来避免控制台缓冲区。

这是该链接的代码:

use Win32::API;

binmode(STDOUT, ":unix:utf8");

#Must set the console code page to UTF8
$SetConsoleOutputCP= new Win32::API( 'kernel32.dll', 'SetConsoleOutputCP', 'N','N' );
$SetConsoleOutputCP->Call(65001);

$line1="\x{2554}".("\x{2550}"x15)."\x{2557}\n";
$line2="\x{2551}".(" "x15)."\x{2551}\n";
$line3="\x{255A}".("\x{2550}"x15)."\x{255D}";
$unicode_string=$line1.$line2.$line3;

print "THIS IS THE CORRECT EXAMPLE OUTPUT IN PURE PERL: \n";
print $unicode_string;

答案 1 :(得分:3)

伙计们:继续研究Perlmonks的帖子,结果证明这更整洁更好: 替换:
use Win32::API;

$SetConsoleOutputCP= new Win32::API( 'kernel32.dll', 'SetConsoleOutputCP', 'N','N' );
$SetConsoleOutputCP->Call(65001);

使用:

use Win32::Console;

 Win32::Console::OutputCP(65001);

保持原封不动。
这更符合Perl的简洁和神奇的精神。

答案 2 :(得分:1)

您还可以使用Win32::Unicode::ConsoleWin32::Unicode::Native在Windows控制台上实现unicode打印。

答案 3 :(得分:0)

此外,在使用ConEmu时不存在此行为,这也可以在Windows的命令控制台中启用适当的Unicode支持。

相关问题