Question

I am trying to run a simple perl code that uses Persian/Arabic characters:

#!/usr/bin/perl
use strict;
use warnings;

print "سلام";

I am using strawberry perl on Windows 7. When I fire up the command line and execute the program, the message shown is garbled. The encoding should be utf-8 to get the message rendered correctly.

So I figured if I use the chcp 65001 command, the problem will be resolved. Unfortunately that didn't help and I still have the same problem.

I tried rewriting the beginning lines to get the correct encoding:

#!/usr/bin/perl

use utf8;
use strict;
use warnings;
use warnings qw(FATAL utf8);
binmode STDOUT, ':encoding(UTF-8)';

print "سلام";

Then I ran perl on command-line and first executed the line chcp 65001 and then ran the program. Here's what I got:

enter image description here

Was it helpful?

Solution

This seems to be related to my recent question and blog posts culminating in Why is PERLIO_F_CRLF set on the bottom-most 'unix' layer on Windows?.

A work-around seems to be to use binmode(STDOUT, ":unix:encoding(utf8):crlf"); to undo the fact that when output is to a cmd.exe window (and only in that case) the bottom-most 'unix' layer has the CRLF flag set (in direct contradiction to perliol).

Instead of cmd.exe, you can use ConEmu which has a lot of nice features and makes it really easy to set up a custom font (in addition to individual tabs with private settings etc).

Don't let what happens in cmd.exe dictate anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top