Probably the first thing to note is that the numeric value of 7-bit ASCII characters is the same as the UTF-8 code points for the same Unicode characters. The ASCII character "A" has the hexidecimal value 41; The UTF-8 character "A" has the hexidecimal value 41.

The term Unicode is often used in a sense to indicate characters outside the range of 7-bit ASCII characters. This often degenerates into arguments over what was said, what was meant, what's techinically correct and so on. For the remainder of this node, assume ASCII refers to 7-bit ASCII characters and Unicode refers to UTF-8 characters outside the range of 7-bit ASCII characters.

In the simplest case, if your Perl source code, input data and output data contain only ASCII characters, there's no need to do anything special. This was the case with your code and data here: ASCII characters were used throughout so no special pragmata or encoding directives were required.

[For the following examples, note that the letter A has a numerical value of 65 decimal (41 hexidecimal) and the smiley face character has a numerical value of 9786 decimal (263a hexidecimal).]

Here's a short piece of Perl code with just ASCII characters:

$ perl -E 'say ord "A"; say sprintf "%x", ord "A"'
65
41

Here's a similar piece of Perl code but this also includes Unicode characters:

$ perl -E 'say ord "☺"; say sprintf "%x", ord "☺"'
226
e2

As you can see, that second example didn't work very well: it produced unexpected results. Because the source code contained Unicode characters, you need to tell Perl this with the utf8 pragma:

$ perl -E 'use utf8; say ord "☺"; say sprintf "%x", ord "☺"'
9786
263a

Here's a short piece of Perl code which outputs ASCII characters:

$ perl -E 'say chr 65; say "\x{41}"'
A
A

Here's a similar piece of Perl code which outputs Unicode characters:

$ perl -E 'say chr 9786; say "\x{263a}"'
Wide character in say at -e line 1.
☺
Wide character in say at -e line 1.
☺

As you can see, that produced warnings; however, if we let Perl know to expect Unicode output with binmode, we get a better result:

$ perl -E 'binmode STDOUT => ":utf8"; say chr 9786; say "\x{263a}"'
☺
☺

Those were just trivial examples. See the documentation for details. I suggest you start with:

And then move on to:

[Note: In order to display Unicode characters exactly as coded or output, I've used <pre>...</pre> and <tt>...</tt> tags instead of <code>...</code> and <c>...</c> tags.]

-- Ken


In reply to Re^3: Suggestions to make this code more Perlish by kcott
in thread Suggestions to make this code more Perlish by ricardo_sdl

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.