perl special char windows

1k Views Asked by At

i try to have german special chars: öäüßÖÄÜ in the output of ma perl console app, but i failed. It's a german win7 system with active codepage 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";
}

I get errors like:

"\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.

How can i get a propper output?

1

There are 1 best solutions below

3
On BEST ANSWER

When using utf8 characters in your source code and using the IO layer for encoding, you should turn on utf8 in the perl parser:

use utf8; 

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

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