I think you have something wrong with your test data. The
ord() function most definitely does correctly handle multi-byte UTF-8 characters. For example this code:
use utf8;
my $euro = "\x{20AC}";
print ord($euro);
print length($euro);
correctly prints 8364 and 1 (even under 5.6). In UTF-8, the Euro symbol
encodes to the three bytes 0xE2 0x82 0xAC but this code:
use utf8;
my $binary = "\xE2\x82\xAC";
print ord($binary);
print length($binary);
prints out 226 and 3. Both strings consist of the same three bytes, but $euro is flagged internally as a UTF-8 string whereas $binary is not.
Remember, Perl has to deal with both character data and binary data. Just because Perl sees the 0xE2 0x82 0xAC sequence of bytes does not mean it should always treat it as a single character.
Here are a few ways to enter UTF-8 encoded non-ASCII characters:
- Use a UTF-8 aware editor (eg: VIM) and just type them in to string literals (or even variable names)
- Use the Unicode escape sequence \x{XXXX} in a double quoted string (see above)
- Read some data in from an XML file using a proper XML Parser module
For more details see the utf8 man page.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.