Perl: printing Unicode strings to the Windows console

后端 未结 4 1292
独厮守ぢ
独厮守ぢ 2021-01-04 23:02

I am encountering a strange problem in printing Unicode strings to the Windows console*.

Consider this text:

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

Intermediary

היא רוצה ל         


        
相关标签:
4条回答
  • 2021-01-04 23:49

    Did you try the solution from perlmonk ?

    It use :unix as well to avoid the console buffer.

    This is the code from that link:

    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;
    
    0 讨论(0)
  • 2021-01-04 23:49

    Also, this behaviour is not present while using ConEmu, which also enables proper Unicode support in Windows' command console.

    0 讨论(0)
  • 2021-01-04 23:54

    You can also utilize Win32::Unicode::Console or Win32::Unicode::Native to achieve unicode prints on windows console.

    0 讨论(0)
  • 2021-01-05 00:01

    Guys: continuing on studying that Perlmonks post, turns out that this is even neater and nicer: replace:
    use Win32::API;
    and:

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

    with:

    use Win32::Console;
    

    and:

     Win32::Console::OutputCP(65001);
    

    Leaving all else intact.
    This is even more in the spirit of Perl conciseness and magic.

    0 讨论(0)
提交回复
热议问题