|The stupid question is the question not asked|
text encodings and perlby andal (Friar)
|on Nov 12, 2010 at 13:28 UTC||Need Help??|
The text in different languages can be represented in computer by different sequences of bytes. This is well known fact (hopefully).
It appears that quite often this introduces confusion. Below is an attempt to describe the basics of encodings handling in perl as I see it after struggling through various documents.
First of all, one should be aware of the difference between "internal form" of a string and sequence of octects. If the string is in "internal form" then perl attempts to find "characters" in it. Otherwise, perl simply works with "octets". The data for some string can be obtained from different sources. If those sources are external to perl, then perl can't know how to identify characters there (the encoding is not known to perl). So the programmer may help here using the module Encode.
The function Encode::decode is used to tell perl what is the encoding used by the "octets", so that perl can construct "internal form" of the string.
The function Encode::encode is used for the reverse operation. When you need to store some "internal form" string into external storage, then you must convert it back to octects with desired encoding.
To see if some string is the "internal form" one can use Encode::is_utf8 function. Note, it is not so important which encoding is used by the "internal form". It can be any. Important is only that it is "internal", so it shouldn't be passed to external entities.
These are the basics. There are few "short-cuts" for going from "octets" to "internal form" strings and back, like binmode(":encoding(foo)") or "use utf8", but they essentially do what Encode does to strings.
These basics helped me to understand things written in "perldoc perlunicode", "perldoc utf8", "perldoc encoding" etc. I hope that this might be of some help to others as well.