首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >binmode(STDOUT,“:utf8 8”);和Strawberry perl中的Unix行尾

binmode(STDOUT,“:utf8 8”);和Strawberry perl中的Unix行尾
EN

Stack Overflow用户
提问于 2019-04-17 15:52:15
回答 1查看 249关注 0票数 0

使用StrawberryPerlv5.28.1,在Windows 10上,我试图实现与Linux相同的结果--即获得带有Unix行尾的UTF8编码文件

下面是我的Perl脚本:

代码语言:javascript
复制
#!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";
        }
}

这是我的输入文件:

代码语言:javascript
复制
ААК|Плоскодонное речное судно
ААРОНОВЕЦ|
ААРОНОВЩИНА|
ААТ|Драгоценный красный камень в Японии
АБА|Толстое и редкое белое сукно
АБАЖУР|
АБАЖУРОДЕРЖАТЕЛЬ|
АБАЗ|Грузинская серебряная монета
АБАЗА|

下面是我如何运行它(我使用type而不是<,因为在我的实际用例中有很多输入文件):

代码语言:javascript
复制
type input.txt | perl encode-words-ru.pl > output.txt

不管我在上面的Perl源代码中尝试了什么,output.txt中的行都由\x0D\x0A终止。

请帮助我阻止perl“帮助”我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-17 16:39:49

可能有更好的方法,但您可以将STDOUT作为:raw文件句柄,然后自己对输出进行编码。

代码语言:javascript
复制
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";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55731584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档