0

我尝试在 ma perl 控制台应用程序的输出中使用德语特殊字符:öäüßÖÄÜ,但我失败了。这是一个带有活动代码页 850 的德国 win7 系统。

#!/usr/bin/perl 
use warnings;
use strict;

binmode(STDOUT , ":encoding(cp437)" )  if $^O eq 'MSWin32';
#binmode(STDOUT , ":encoding(cp850)" )  if $^O eq 'MSWin32';
#binmode(STDOUT , ":encoding(cp1252)" ) if $^O eq 'MSWin32';

my @sp_chars = qw/ä ö ü ß Ä Ö Ü/;

foreach my $sp_char ( @sp_chars ) {
  print "$sp_char\n";
}

我收到如下错误:

"\x{009f}" does not map to cp1252 at umlaute.pl line 12.

"\x{009f}" does not map to cp850 at umlaute.pl line 12.

"\x{00c3}" does not map to cp437 at umlaute.pl line 12.

我怎样才能得到正确的输出?

4

1 回答 1

1

在源代码中使用 utf8 字符并使用 IO 层进行编码时,应在 perl 解析器中打开 utf8:

use utf8; 

my $encoding = $^O eq 'MSWin32' ? 'cp850' : 'utf8';
binmode(STDOUT, ":encoding($encoding)" );

print "$_\n" for qw/ä ö ü ß Ä Ö Ü/;
于 2015-02-02T19:44:41.750 回答