hexcoder has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use Win32::Console::Ansi; # converts output for code page 850 to OEM c +ode page my $isRedirected = ! -t STDOUT; my $Str = 'äöüÄÖÜß' . "\n"; # this string has default encoding iso-885 +9-1 # if STDOUT is redirected to a file, use unicode encoding, otherwise u +se default if ($isRedirected) { if (!defined binmode STDOUT, ':encoding(UTF-8)') { warn "binmode failed: $!\n"; } utf8::encode($Str); } # output string with a hexdump if (!print 'string: ', hd($Str), $Str){ warn "print failed\n"; } # With a redirected file I end up with a UTF-16 LE BOM encoded file wi +th wrong content :-( # for comparison, this works as expected (produces utf8 content) open(my $fh, '>:encoding(UTF-8)', 'utf8_2') or die "can't open file fo +r writing:$!\n"; print $fh $Str or warn "print to file failed\n"; close $fh or warn "close file failed\n"; # hexdump sub hd { my $input = shift; return join(' ', unpack('(H2)*', $input)), "\n"; }
while the redirected STDOUT output looks very different.000000 c3 a4 c3 b6 c3 bc c3 84 c3 96 c3 9c c3 9f 0d 0a
The second line has this hexdump:string: e4 f6 fc c4 d6 dc df 0a ├ñ├Â├╝├ä├û├£├ƒ
What happened here? How did I end up with a UTF-16 LE BOM version? What would you suggest to obtain UTF8 encoding for the redirected file? Thanks for any enlightenment!000000 1c 25 f1 00 1c 25 c2 00 1c 25 5d 25 1c 25 e4 00 000010 1c 25 fb 00 1c 25 a3 00 1c 25 92 01 0d 00 0a 00
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: binmode(':encoding(UTF-8)') did not produce utf8 for me
by ikegami (Patriarch) on Jul 03, 2023 at 23:26 UTC | |
by hexcoder (Curate) on Jul 04, 2023 at 17:37 UTC | |
by ikegami (Patriarch) on Jul 05, 2023 at 03:29 UTC | |
by Anonymous Monk on Jul 06, 2023 at 19:43 UTC | |
by ikegami (Patriarch) on Jul 09, 2023 at 10:57 UTC | |
|
Re: binmode(':encoding(UTF-8)') did not produce utf8 for me
by NERDVANA (Priest) on Jul 04, 2023 at 01:02 UTC |