如何在Perl中打印Hexagram?

Dou*_*rsh 3 windows unicode perl utf-8

我正在尝试在Perl中从此处打印第一个卦。

下面的代码不会产生任何错误,但是也不会打印任何六边形。

use warnings;
use open ':encoding(utf8)';
binmode(STDOUT, ":utf8");

print "\x{4DC0}\n";
Run Code Online (Sandbox Code Playgroud)

我希望看到这个“?” 不是“ ??Ç”。

ike*_*ami 5

You tell Perl your terminal is expecting UTF-8, but your terminal appears to expect one of the following:[1]

Seeing as these are all Windows code pages, I presume the terminal in question is a Windows console. If so, you can find out which encoding is expected using either of these commands:

chcp
Run Code Online (Sandbox Code Playgroud)

perl -le"use Win32; print Win32::GetACP()"
Run Code Online (Sandbox Code Playgroud)

cp在数字前面加上一个名称,即可与编码模块(该:encoding图层使用)一起使用。

但是,知道期望的编码不会帮助您。这些编码的字符集都不包含“?”,因此您的终端无法显示“?”。没有变化。

您可以通过发出以下命令,将Windows控制台期望的编码切换为UTF-8:

chcp 65001
Run Code Online (Sandbox Code Playgroud)

您可能必须在控制台的属性中调整字体。


  1. 我使用以下程序获得了可能的编码列表:

    use strict;
    use warnings;
    use feature qw( say );
    use utf8;
    use Encode qw( decode encode_utf8 );
    
    my $output = encode_utf8("\x{4DC0}");
    my $displayed = "??Ç";
    
    for my $encoding (Encode->encodings(":all")) {
       defined( my $got = eval { decode($encoding, $output, Encode::FB_CROAK|Encode::LEAVE_SRC) } )
          or next;
    
       say $encoding if $output eq $displayed;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    (确保文件使用UTF-8编码。)