Perl is supposed to treat 0xFC as a single byte.
No it's not. There are actually basically two different encodings for the same character: As one byte "\xFC" (ISO-Latin-1 string), and as 2 UTF-8 bytes, "\xC3\xBC" (UTF-8 string). That's one of the most annoying things about Perl's "smart" automatic UTF-8 processing: that it occasionally does the wrong thing, i.e. not what you mean, and that you're left to pick up the pieces.
These strings are actually marked as different, internally: the latter will have the UTF-8 flag set, the former, not. Perl can turn the former into the latter by joining it with a UTF-8 marked string. I'm not totally convinced that is something to cheer at. But anyway: it may even be a zero length string.
$x = "\xFC"; # ISO-Latin-1: 1 byte
$x .= pack 'U0'; # append a zero length UTF-8 string
# which will turn the whole thing into UTF-8!
# hex dump:
local($\, $,) = ("\n", " ");
print map { sprintf "%02X", $_ } unpack 'C*', $x;
Result:
C3 BC
Using tricks like these, one can force perl into submission, even without use bytes (works even for pre 5.8 perls):
$l1 = pack 'C0a*', $s; # copies the bytes out of $s and marks the r
+esult as non-UTF-8
$u8 = pack 'U0a*', $s; # copies the bytes out of $s and marks the r
+esult as UTF-8 -- even if the bytes don't form proper UTF-8 strings!
|