Daniel Rusev
Daniel Rusev

Reputation: 1331

Reading Cyrillic characters from file in perl

I'm having trouble reading Cyrillic characters from a file in perl.

The text file is written in Notepad and contains "абвгдежзийклмнопрстуфхцчшщъьюя". Here's my code:

#!/usr/bin/perl

use warnings;
use strict;

open FILE, "text.txt" or die $!;

while (<FILE>) {
    print $_;   
}

If I save the text file using the ANSI encoding, I get:

рстуфхцчшщъыьэюяЁёЄєЇїЎў°∙·№■

If I save it using the UTF-8 encoding, and I use the function decode('UTF-8', $_) from the package Encode, I get:

Wide character in print at test.pl line 11, <TEXT> line 1.

and a bunch of unreadable characters.

I'm using the command prompt in windows 7x64

Upvotes: 2

Views: 2144

Answers (1)

ikegami
ikegami

Reputation: 385917

You're decoding your inputs, but "forgot" to encode your outputs.

Your file is probably encoded using cp1251.

Your terminal expects cp866.

Use

use open ':std', ':encoding(cp866)';
use open IO => ':encoding(cp1251)';
open(my $FILE, '<', 'text.txt')
   or die $!;

or

use open ':std', ':encoding(cp866)';
open(my $FILE, '<:encoding(cp1251)', 'text.txt')
   or die $!;

Use :encoding(UTF-8) instead of :encoding(cp1251) if you saved as UTF-8.

Upvotes: 5

Related Questions