简体   繁体   English

如何在Perl管道上设置编码?

[英]How can I set the encoding on a Perl pipe?

Is "'|-:encoding(utf-8)'" in "open my $column, '|-:encoding(utf-8)', qw(column);" 在“打开我的$ column,'|-:encoding(utf-8)',qw(column);”中是“'|-:encoding(utf-8)'” ok? 好? It seems to work almost. 它似乎几乎可以工作。 In the row with the "Mikołaj" the formatting is not correct; 在带有“Mikołaj”的行中,格式不正确; how can I fix it? 我该如何解决?

#!/usr/bin/perl
use warnings;
use strict;
use utf8;
binmode STDOUT, ':encoding(utf-8)';
# edit:
binmode DATA, ':encoding(utf-8)';

open my $column, '|-:encoding(utf-8)', qw(column);
print $column "$_\n" for <DATA>;

__DATA__
Julius
Giovanni
George
Alonso
Jerome
Eugène
Reinhold
Gustav
Pavel
Franz
Max
Mikołaj
Maurice
Alec
Émile
Xaver
Henry
Wolfgang
Alexander
Enrico
Bedrich
Joaquín
Bartholomäus
Otto
Carl
Isaak
Anatol
Leos
Herbert
Paul
Benjamin
Matthew
Jean

You talk about formatting and evidently want aligned output. 您谈论格式,并且显然想要对齐的输出。 I notice that ł is the only character in the sample not occurring in Latin1. 我注意到ł是样本中唯一不在Latin1中出现的字符。 To rule out the problem that you just have a stupid font, configure your terminal to display a well-behaved one with extensive coverage: DejaVu Sans Mono is nice. 要排除您只有愚蠢字体的问题,请将您的终端配置为显示行为良好且覆盖面广的终端: DejaVu Sans Mono很不错。

Also, have you hexdumped the output yet? 另外,您是否已将输出十六进制转储? This is what I get: 这是我得到的:

0000  4d 69 6b 6f c5 82 61 6a  0a                       Miko..aj .

This example script works correctly for me in BSD/OSX. 这个示例脚本在BSD / OSX中对我来说正常工作。 Usually when there's an issue with formatting it's an encoding mismatch somewhere. 通常,当格式存在问题时,则是某处的编码不匹配。 Make sure: 确保:

  • the file is being saved in UTF8 format (check whatever text editor you're using) 文件以UTF8格式保存(检查您使用的文本编辑器)
  • your terminal handles UTF8 (view the file in the terminal, should appear correctly) 您的终端处理UTF8(在终端中查看文件,应正确显示)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM