Hello, Perl Monks! Long-time listener, first-time caller. Despite nearly thirty years of Perl work, there are a few things I hate so much I actively avoid understanding them, and because I tend to always need fast solutions yesterday, there have been a number of times where I've gone for a brute-force approach that isn't very maintainable in the long term.

I get lumps of long text data that have what I, an Old, think of as "extended ASCII characters" or "special characters." I apparently get them in UTF-8. For data output for web page purposes, I must find any extended characters in the data and attempt to translate them to Unicode. My preference would be to translate them to HTML escaped entities, but the client wants Unicode.

My brute-force approach is, for the various data fields which require translation, I call a function of my own:
$answer = &blunt_decode($answer);

The subroutine blunt_decode() consists literally of lines and lines of regex substitutions. Not more than two Unicode codepages' worth, I don't think, covering ninety percent of the extended characters that show up in names (it's always proper names that are the problem), but it's still a lot of lines. e.g.:

$s =~ s/\xc4\x80/\x{100}/g; $s =~ s/\xc4\x81/\x{101}/g; $s =~ s/\xc4\x82/\x{102}/g; $s =~ s/\xc4\x83/\x{103}/g; $s =~ s/\xc4\x84/\x{104}/g; $s =~ s/\xc4\x85/\x{105}/g; $s =~ s/\xc4\x86/\x{106}/g; $s =~ s/\xc4\x87/\x{107}/g; $s =~ s/\xc4\x88/\x{108}/g; $s =~ s/\xc4\x89/\x{109}/g; $s =~ s/\xc4\x8a/\x{10A}/g; $s =~ s/\xc4\x8b/\x{10B}/g; $s =~ s/\xc4\x8c/\x{10C}/g; $s =~ s/\xc4\x8d/\x{10D}/g;

And so on and so on. You get the idea. I know there has got to be a better way that doesn't require me to essentially maintain my own lookup table, but I don't know what it is and when I start reading about Unicode translation my eyes glaze over and my brain gets fuzzy.

I did look at Encode::Unicode and tried using that, but I must have been doing it wrong, because

use Encode qw(encode decode); $answer = encode("UCS-2BE", $answer);
mangled the data output horribly.

In short, I am looking for some canned, easier way to accept a scalar, translate any extended-character UTF8 into the equivalent unicode sequence (leaving the normal characters alone), and spit the translated scalar back.


In reply to UTF-8 and Unicode the hard way by Anonymous Monk

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.