in reply to binmode(':encoding(UTF-8)') did not produce utf8 for me
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: binmode(':encoding(UTF-8)') did not produce utf8 for me
by hexcoder (Curate) on Jul 04, 2023 at 17:37 UTC | |
by ikegami (Patriarch) on Jul 05, 2023 at 03:29 UTC | |
|
Re^2: binmode(':encoding(UTF-8)') did not produce utf8 for me
by Anonymous Monk on Jul 06, 2023 at 19:43 UTC | |
by ikegami (Patriarch) on Jul 09, 2023 at 10:57 UTC |