perlboy_emeritus:

The use utf8; directive only tells perl that you're using Unicode in your source code file. It doesn't tell perl to perform any automatic conversions.

The documentation (perldoc -f chr) explicitly states that chr doesn't encode the characters 128..255 (which includes 0xB0) as UTF-8 internally for backward compatibility reasons.

However, if you tell perl to add the utf8 encoding to the output stream, then the 0xb0 will be encoded on output as you want:

$ perl -e 'binmode STDOUT,":utf8"; print chr(0xb0),"\n"' °

Update: I'm not really all that comfortable with Unicode stuff, so reaching for Devel::Peek, I fabricobbled this little thing together:

$ cat pm1208450.pl use strict; use warnings; use Devel::Peek; my $a = chr(0xb0); my $b = chr(0x2032); Dump($a); Dump($b); # Combining a byte string and a unicode string converts to unicode my $c = $a . $b; Dump($c); $ perl pm1208450.pl SV = PV(0x60002c270) at 0x600079168 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x600069e70 "\260"\0 CUR = 1 LEN = 10 SV = PV(0x60002c310) at 0x600079048 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x60008f670 "\342\200\262"\0 [UTF8 "\x{2032}"] CUR = 3 LEN = 10 SV = PV(0x60002c340) at 0x6000ed1c8 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x600093a10 "\302\260\342\200\262"\0 [UTF8 "\x{b0}\x{2032}"] CUR = 5 LEN = 10

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.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Potential bug in chr by roboticus
in thread Potential bug in chr by perlboy_emeritus

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.