我需要读取以 iso-8859-1 编码的文件。
出于某种原因,我无法让编码层(如 PerlIO::encoding
中所述)工作。这是我正在做的一个最小的例子。test.txt
包含以 iso-8859-1 编码的单个英镑符号。
% iconv -f iso-8859-1 test.txt
£
% hexdump -C test.txt
00000000 a3 0a |..|
00000002
我的 Perl 脚本:
#!/bin/perl
use warnings;
use strict;
open my $f, "<:encoding(iso-8859-1)", $ARGV[0] or die qq{Could not open $ARGV[0]: $!};
while (<$f>) {
print;
}
结果:
% ./script.pl test.txt | hexdump -C
00000000 a3 0a |..|
00000002
所以脚本打印它读取的确切字节序列,没有
进行了转换。
最佳答案
我假设未使用特定编码声明的文件句柄默认使用 utf-8 编码,但显然这不是真的。
添加显式
binmode(STDOUT, ":utf8");
解决问题。
关于perl - 为什么 Perl 的编码层没有任何作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49094348/