perl特殊的char窗口

时间:2015-02-02 19:15:29

标签: perl codepages binmode

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

#!/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.

如何获得propper输出?

1 个答案:

答案 0 :(得分:1)

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

use utf8; 

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

print "$_\n" for qw/ä ö ü ß Ä Ö Ü/;