问题描述
我遇到一个奇怪的问题打印Unicode字符串到Windows控制台*。
I am encountering a strange problem in printing Unicode strings to the Windows console*.
请考虑这个文本:
אני רוצה לישון
Intermediary
היא רוצה לישון
אתם, הם
Bye
Hello, world!
test
假设它在一个名为file.txt的文件中。
Assume it's in a file called "file.txt".
当我去*:type file.txt,它打印出来。但是当它从Perl程序打印时,像这样:
When I go*: "type file.txt", it prints out fine. But when it's printed from a Perl program, like this:
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行(本例中为希伯来语)破损,像这样:
The Unicode lines (Hebrew in this case) show up again each time, partially broken, like this:
E:\My Documents\Technical\Perl>perl "hello unicode.pl"
אני רוצה לישון
לישון
�ן
Intermediary
היא רוצה לישון
לישון
�ן
אתם, הם
�ם
Bye
Hello, world!
test
(我将所有内容保存为UTF-8)。
(I save everything in UTF-8).
这是很奇怪的。任何建议?
This is mighty strange. Any suggestions?
(这不是一个控制台2问题* - 同样的问题出现在常规的Windows控制台上,只有你没有看到希伯来语字形)。
(It's not a "Console2" problem* - the same problem shows up on a "regular" windows console, only there you don't see the Hebrew glyphs).
*使用控制台(也称为控制台2) - 这是一个很好的小实用程序,与Windows控制台使用Unicode - 请参见例如:
* Using "Console" (also called "Console2") - it's a nice little utility which enables working with Unicode with the Windows console - see, for example, here:http://www.hanselman.com/blog/Console2ABetterWindowsCommandPrompt.aspx
**注意:在控制台,你必须说,当然是:
** Note: at the console, you have to say, of course:
chcp 65001
推荐答案
您是否尝试过的解决方案?
它使用:unix
以避免控制台缓冲区。
It use :unix
as well to avoid the console buffer.
该链接的代码:
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;
这篇关于Perl:将Unicode字符串打印到Windows控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!