use strict; use warnings; use Win32::Console::Ansi; # converts output for code page 850 to OEM code page my $isRedirected = ! -t STDOUT; my $Str = 'äöüÄÖÜß' . "\n"; # this string has default encoding iso-8859-1 # if STDOUT is redirected to a file, use unicode encoding, otherwise use 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 with 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 for 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"; } #### 000000 c3 a4 c3 b6 c3 bc c3 84 c3 96 c3 9c c3 9f 0d 0a #### string: e4 f6 fc c4 d6 dc df 0a ├ñ├Â├╝├ä├û├£├ƒ #### 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