in reply to Re: Potential bug in chr
in thread Potential bug in chr

Thanks. Got it. I'll include that binmode expression whenever I'm working with external UTF-8 data and need an accurate debugging environment. I already know to assert "<:encoding(UTF-8)" on input file handles but overlooked STDOUT.

Replies are listed 'Best First'.
Re^3: Potential bug in chr
by ikegami (Patriarch) on Feb 05, 2018 at 01:31 UTC
    use open ':std', ':encoding(UTF-8)';
    makes far more sense than
    binmode STDOUT, ':utf8';

    It binmodes STDIN, STDOUT and STDERR (with the safer :encoding(UTF-8)). It also sets the default encoding for the instances of open in the scope (making the :encoding('UTF-8') redundant in the open).


    This shows that if you happen to join a byte-oriented string with a unicode string in perl, the result will be a unicode string.

    Which is irrelevant to the question at hand.

    The first print worked because the string contained non-bytes (chars outside of 0..255), which can't be printed without encoding. perl guessed that you meant to encode them using UTF-8 (and warns you about this ("Wide character in...")).

    perl had no way of knowing the second print was wrong because it only contained bytes (chars in 0..255), so it printed the string unaltered.