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.


In reply to text encodings and perl by andal

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.