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

Hel*_*man 8 unicode console perl utf-8 unicode-string

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

考虑这个文字:

??? ???? ?????

Intermediary

??? ???? ?????
???, ??
Bye
Hello, world!
test
Run Code Online (Sandbox Code Playgroud)

假设它位于名为"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;
 }
Run Code Online (Sandbox Code Playgroud)

Unicode行(在本例中为希伯来语)每次都会再次显示,部分打破,如下所示:

E:\My Documents\Technical\Perl>perl "hello unicode.pl"
??? ???? ?????
?????
??

Intermediary
??? ???? ?????
?????
??

???, ??
??

Bye
Hello, world!
test
Run Code Online (Sandbox Code Playgroud)

(我用UTF-8保存所有内容).

这太奇怪了.有什么建议?

(这不是"Console2"问题* - 同样的问题出现在"常规"Windows控制台上,只有你没有看到希伯来字形).


*使用"Console"(也称为"Console2") - 这是一个很好的小实用程序,可以使用Windows控制台使用Unicode - 例如,请参阅:http: //www.hanselman.com/blog/Console2ABetterWindowsCommandPrompt.aspx

**注意:在控制台,您必须说,当然:

chcp 65001
Run Code Online (Sandbox Code Playgroud)

J-1*_*DiZ 5

您是否尝试过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;
Run Code Online (Sandbox Code Playgroud)