当Perl程序在cmd.exe中输出UTF-8编码的字符串时,为什么要重复最后一个八位字节? 更新
正如@ikegami所建议的,我报告这是一个bug 考虑以下C和Perl程序,它们都在标准输出上输出字符串“αβγ”的UTF-8编码: C版本: 另一方面,避免了这个问题 C:\…> perl -e "syswrite STDOUT, qq{\xce\xb1\xce\xb2\xce\xb3xyz\n}" αβγxyz C:\…>perl-e“打印qq{\xce\xb1\xce\xb2\xce\xb3}”当Perl程序在cmd.exe中输出UTF-8编码的字符串时,为什么要重复最后一个八位字节? 更新,c,windows,perl,utf-8,C,Windows,Perl,Utf 8,正如@ikegami所建议的,我报告这是一个bug 考虑以下C和Perl程序,它们都在标准输出上输出字符串“αβγ”的UTF-8编码: C版本: 另一方面,避免了这个问题 C:\…> perl -e "syswrite STDOUT, qq{\xce\xb1\xce\xb2\xce\xb3xyz\n}" αβγxyz C:\…>perl-e“打印qq{\xce\xb1\xce\xb2\xce\xb3}” αβγ�γ� 以下程序产生正确的输出: use utf8; use str
αβγ�γ� 以下程序产生正确的输出:
use utf8;
use strict;
use warnings;
use warnings qw(FATAL utf8);
binmode(STDOUT, ":unix:encoding(utf8):crlf");
print 'αβγxyz', "\n";
输出:
C:\…> chcp 65001
Active code page: 65001
C:\…> perl pttt.pl
αβγxyz
给我:
---
- unix
- ''
- 0x01205200
- crlf
- ''
- 0x00c85200
- unix
- ''
- 0x01201200
- encoding
- utf8
- 0x00c89200
- crlf
- ''
- 0x00c8d200
αβγxyz
正如所料,
unix
层没有设置CRLF
标志。我的猜测:windows外壳中的某个地方有一个bug。@FilipeGonçalves鉴于C版本和syswrite
都按预期工作,我怀疑这是Perl的:CRLF
IO层和代码页65001之间的交互。但是,我不知道该去哪里找。欢迎使用指针。使用perlbug
提交错误报告如果省略\n
会发生什么?在windows的文本模式下,stdout
是否将'\n
\翻译成“\r\n”
?我不知道这是怎么解释的,但是另一个想法。你可能想试试我的PerlIO::Layers,而不是使用PerlIO::get_Layers,它提供了更友好的输出。:crlf
层中的一个bug?你肯定是在开玩笑/s@Ether在我看来,当输出到cmd.exe时,它就像Windows上unix层中的一个bug。但是,我不明白为什么。
C:\…> perl -e "syswrite STDOUT, qq{\xce\xb1\xce\xb2\xce\xb3xyz\n}"
αβγxyz
C:\…> chcp 437
Active code page: 437
C:\…> cttt.exe
╬▒╬▓╬│
C:\…> perl -e "print qq{\xce\xb1\xce\xb2\xce\xb3\n}"
╬▒╬▓╬│
C:\…> perl -e "print qq{\xce\xb1\xce\xb2\xce\xb3xyz}"
αβγxyzxyz
C:\…> perl -e "print qq{\xce\xb1\xce\xb2\xce\xb3}"
αβγ�γ�
use utf8;
use strict;
use warnings;
use warnings qw(FATAL utf8);
binmode(STDOUT, ":unix:encoding(utf8):crlf");
print 'αβγxyz', "\n";
C:\…> chcp 65001
Active code page: 65001
C:\…> perl pttt.pl
αβγxyz
binmode(STDOUT, ":unix:encoding(utf8):crlf");
print Dump [
map {
my $x = defined($_) ? $_ : '';
$x =~ s/\A([0-9]+)\z/sprintf '0x%08x', $1/eg;
$x;
} PerlIO::get_layers(STDOUT, details => 1)
];
print "αβγxyz\n";
---
- unix
- ''
- 0x01205200
- crlf
- ''
- 0x00c85200
- unix
- ''
- 0x01201200
- encoding
- utf8
- 0x00c89200
- crlf
- ''
- 0x00c8d200
αβγxyz
---
- unix
- ''
- 0x00201200
- crlf
- ''
- 0x00405200
- encoding
- utf8
- 0x00409200