First, you claim you source code contains

my $Str = 'äöüÄÖÜß' . "\n";

Perl expects source encoded using UTF-8 when use utf8; is in effect.
Perl expects source encoded using ASCII when use utf8; isn't.

Seeing as you didn't use use utf8;, and seeing as those characters aren't ASCII characters, your script couldn't possibly contain those characters.

Based on the hd output, it appears that your source code is encoded using ISO-8859-1 as you say, or more likely Windows-1252 since you're on a Windows machine.


Secondly, the Win32 console is extremely unlikely to want iso-8859-1 or Windows-1252. "cp" . Win32::GetConsoleOutputCP() will produce the correct encoding.


If your script is encoded using Windows-1252 or ISO-8859-1 (as it currently is), you want this:

use v5.14; use warnings; use Encode qw( decode ); use Win32 qw( ); my $enc = -t STDOUT ? "cp" . Win32::GetConsoleOutputCP() : "UTF-8"; binmode( STDOUT, ":encoding($enc)" ); my $str = decode( "cp1252", "äöüÄÖÜß" ); #warn sprintf "%vX", $str; # E4.F6.FC.C4.D6.DC.DF say $str;

If your script is encoded using UTF-8 (the modern standard), you want this:

use v5.14; use warnings; use utf8; use Win32 qw( ); my $enc = -t STDOUT ? "cp" . Win32::GetConsoleOutputCP() : "UTF-8"; binmode( STDOUT, ":encoding($enc)" ); my $str = "äöüÄÖÜß"; #warn sprintf "%vX", $str; # E4.F6.FC.C4.D6.DC.DF say $str;

Either way, you get this:

>chcp Active code page: 65001 # My machine use UTF-8 for the console. >perl a.pl äöüÄÖÜß >chcp 850 Active code page: 850 # It used to default to this. >perl a.pl äöüÄÖÜß >chcp 437 Active code page: 437 # Common in the US. >perl a.pl äöüÄÖÜß >perl a.pl >a >perl -Mv5.14 -ne"say sprintf '%vX', $_" a C3.A4.C3.B6.C3.BC.C3.84.C3.96.C3.9C.C3.9F.A # UTF-8 of äöüÄÖÜß

See Re: [OT] ASCII, cmd.exe, linux console, charset, code pages, fonts and other amenities.


In reply to Re: binmode(':encoding(UTF-8)') did not produce utf8 for me by ikegami
in thread binmode(':encoding(UTF-8)') did not produce utf8 for me by hexcoder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.