1

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?

Pete
  • 25
  • 2

1 Answers1

2

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/ä ö ü ß Ä Ö Ü/;
Ben Grimm
  • 4,316
  • 2
  • 15
  • 24
  • Hi Ben, "comming from your source code" was the hint i needed. I missed the point, that notepad++ was set to utf8. Thank you very much! Pete – Pete Feb 02 '15 at 20:06
  • @Pete: The proper solution is to *tell* Perl that the source is in UTF-8 by adding `use utf8` as the first statement in your source file. It's unusual now to use code page 850 (DOS Latin 1) as ISO-8859-1 (code page 1252 apart from a few trivia) has largely superseded it. You may find it simpler to use UTF-8 throughout, which is supported via Windows code page 65001. – Borodin Feb 02 '15 at 21:04
  • Right, if you intend to use the encoding layer anywhere in your program your data must be consistently represented. It's usually _easier_ to treat everything as bytes, so I don't typically turn on utf8, even when I've got utf8 characters in the source. But I'll update the answer since it's the more modern way to do it :) – Ben Grimm Feb 02 '15 at 21:20