4

I have a problem with my Perl scripts. In UNIX-like systems it prints out all Unicode characters like ä properly to the console. In the Windows commandline, the characters are broken to senseless glyphs. Is there a simple way to avoid this? I'm using use utf8;.

Thanks in advance.

dda
  • 6,030
  • 2
  • 25
  • 34
Martin
  • 381
  • 4
  • 17

1 Answers1

13

use utf8; simply tells Perl your source is encoded using UTF-8.

It's not working on unix either. There are some strings that won't print properly (print chr(0xE9);), and most that do will print a "Wide character" warning (print chr(0x2660);). You need decode your inputs and encode your outputs.

In unix systems, that's usuaully

use open ':std', ':encoding(UTF-8)';

In Windows system, you'll need to use chcp to find the console's character page. (437 for me.)

use open ':std', ':encoding(cp437)';  # Encoding used by console
use open IO => ':encoding(cp1252)';   # Encoding used by files
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Trying this out in Windows 7 cmd.exe, it only produced errors. `Odd number of elements in hash assignment at C:/strawberry/perl/lib/encoding.pm line 120. encoding: Unknown encoding ':std' at C:\Users\Peter\perl\foo.pl line 9.` (same for `:encoding...` and `IO => ..`). What I finally got to work was changing the cmd.exe font to Lucida console, chcp 1252, then use `use encoding 'cp1252';` inside the script. – TLP Mar 05 '13 at 13:52
  • @TLP, Fixed. Should be `open`, not `encoding`. Never use `encoding`. – ikegami Mar 05 '13 at 13:56
  • 1
    +1 That works. Wow, so much trouble over printing characters. – TLP Mar 05 '13 at 14:13