使用StrawberryPerlv5.28.1,在Windows 10上,我试图实现与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“帮助”我!
发布于 2019-04-17 16:39:49
可能有更好的方法,但您可以将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";https://stackoverflow.com/questions/55731584
复制相似问题