pshangov has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks, I have trouble outputting strings that contain Cyrillic characters in the windows terminal (cmd.exe). Here is what I get:
my $string = "АБВ";
# $string contains first three characters of Cyrillic alphabet

print $string, "\n";
# prints strage symbols

print ord 'А', ", ", ord 'Б', ", ", ord 'В', "\n";
# prints 192, 193, 194

print chr $_ for (128, 129, 130); print "\n";
# prints "АБВ"

binmode(STDOUT, ":encoding(cp1251)");
print $string, "\n";
# prints:
# "\x{00c0}" does not map to cp1251 at test_cyr.pl line 15.
# "\x{00c1}" does not map to cp1251 at test_cyr.pl line 15.
# "\x{00c2}" does not map to cp1251 at test_cyr.pl line 15.
# \x{00c0}\x{00c1}\x{00c2}

use Encode qw(encode);
my $octets = encode("cp1251", $string);
print $octets, "\n";
# prints "???"

Any clues would be greatly appreciated. Note: Code is in 'pre' rather than 'code' tags in order to preserve the Cyrillic characters.

Replies are listed 'Best First'.
Re: Printing cyrillic strings in cmd.exe
by almut (Canon) on Jan 23, 2007 at 11:29 UTC

    The following works for me:

    my $string = "\x{0410}\x{0411}\x{0412}"; # unicode binmode STDOUT, ":encoding(cp1251)"; print $string, "\n";

    or

    my $string = "\xc0\xc1\xc2"; # cp1251 print $string, "\n";

    (Of course, you also have to set the appropriate code page ("chcp 1251") and use a font that contains the cyrillic glyphs.)

    In case you want to specify the literal strings in your perl code in UTF-8 (rather than as unicode codepoints), you'd have to use utf8 to tell Perl that your script is encoded in UTF-8.

      thanks, the "chcp" command, which I was not aware of, solved the problem!