使用Windows 10上的Strawberry perl v5.28.1,我试图获得与Linux相同的结果 - 即获得带有Unix行结尾的UTF8编码文件。
这是我的Perl脚本:
#!perl -w
use strict;
use utf8;
use Encode qw(encode_utf8);
use Digest::MD5 qw(md5_hex);
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");
my %words;
while(<>) {
# change yo to ye
tr/ёЁ/еЕ/;
# extract russian word and its optional explanation
next unless /^([А-Я]{2,})\|?([А-Я ,-]*)/i;
my ($word, $expl) = (uc $1, $2);
if (length($word) <= 3) {
print $word;
# if explanation is missing, omit the pipe
print (length($expl) > 3 ? "|$expl\x0A" : "\x0A");
} else {
# print the md5 hash and omit the pipe and explanation
print md5_hex(encode_utf8('my secret' . $word)) . "\x0A";
}
}
这是我的输入文件:
ААК|Плоскодонное речное судно
ААРОНОВЕЦ|
ААРОНОВЩИНА|
ААТ|Драгоценный красный камень в Японии
АБА|Толстое и редкое белое сукно
АБАЖУР|
АБАЖУРОДЕРЖАТЕЛЬ|
АБАЗ|Грузинская серебряная монета
АБАЗА|
以下是我运行它的方法(我使用type
而不是<
,因为我的实际用例中有很多输入文件):
type input.txt | perl encode-words-ru.pl > output.txt
无论我在上面的Perl源代码中尝试了什么,output.txt中的行都以\ x0D \ x0A终止
请帮助我阻止perl“帮助”我!
可能有更好的方法,但你可以使STDOUT
成为:raw
文件句柄,然后自己编码输出。
binmode STDOUT; # or binmode STDOUT, ":raw";
...
print (length($expl) > 3 ? encode_utf8("|$expl\n") : "\n"); # $exp1 is already decoded
...
print md5_hex(encode_utf8('my secret' . $word)) . "\n";