On 5.6.x, it can be as simple as
$latin1 = pack 'C*', unpack 'U*', $utf8;
On 5.8.0 (and later), use the Encode module.
Some of the solutions proposed here only work well on pre 5.6 systems, because from 5.6.0 on, perl has built-in magic that automatically converts Latin1 back to UTF-8 (without you asking for it). Like this (on 5.6.1):
$latin1 = 'élève';
$utf8 = chr(8801);
print join ' ', $latin1, $utf8;
Result:
élève ≡
As you can see, the Latin1 is converted into UTF-8. This will render a lot of the code that used to work on 5.005 and earlier, useless: you can't turn UTF-8 to Latin1, as perl will undo your replacements.
The mechanism that is behind all that, is that each string has a flag attached to it, much like the taint flag, indicating whether a string is in UTF-8 or in bytes. When you join strings of bytes to strings in UTF-8, perl will convert the bytes strings to UTF-8. The end string is marked as UTF-8 as well. Personally I really really hate this behaviour.
There are ways around it: in 5.6, using pack, you can turn a string to bytes or to UTF-8, without the bytes themselves being touched, effectively only setting or clearing this UTF-8 flag on the resulting string.
$bytes = pack 'C0a*', $utf8;
$utf8 = pack 'U0a*', $bytes;
See the docs on pack for 5.6.1. Search for "C0".
5.8 has less hackish methods built in. See utf8 and Encode.
|