从perl中的文件中读取西里尔字符

Dan*_*sev 1 perl encoding file

我无法从perl中的文件中读取西里尔字符.

文本文件用记事本编写,包含"абвгдежзийклмнопрстуфхцчшщъьюя".这是我的代码:

#!/usr/bin/perl

use warnings;
use strict;

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

while (<FILE>) {
    print $_;   
}
Run Code Online (Sandbox Code Playgroud)

如果我使用ANSI编码保存文本文件,我得到:

????????????????????????°?·??
Run Code Online (Sandbox Code Playgroud)

如果我使用UTF-8编码保存它,并且我使用Encode包中的函数decode('UTF-8',$ _),我得到:

Wide character in print at test.pl line 11, <TEXT> line 1.
Run Code Online (Sandbox Code Playgroud)

还有一堆不可读的角色.

我在Windows 7x64中使用命令提示符

ike*_*ami 5

您正在解码输入,但"忘记"对输出进行编码.

您的文件可能使用cp1251进行编码.

您的终端需要cp866.

使用

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

要么

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

如果您保存为UTF-8,请使用:encoding(UTF-8)而不是:encoding(cp1251).