in reply to Potential bug in chr
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Potential bug in chr
by perlboy_emeritus (Scribe) on Feb 05, 2018 at 01:25 UTC | |
by ikegami (Patriarch) on Feb 05, 2018 at 01:31 UTC |