Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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
mangled the data output horribly.use Encode qw(encode decode); $answer = encode("UCS-2BE", $answer);
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: UTF-8 and Unicode the hard way
by Corion (Patriarch) on May 09, 2022 at 16:29 UTC | |
by Anonymous Monk on May 09, 2022 at 16:49 UTC | |
by haj (Vicar) on May 09, 2022 at 20:51 UTC | |
by Anonymous Monk on May 10, 2022 at 17:26 UTC | |
by hippo (Archbishop) on May 10, 2022 at 17:38 UTC | |
| |
by Anonymous Monk on May 10, 2022 at 17:32 UTC | |
| |
by Corion (Patriarch) on May 09, 2022 at 17:54 UTC | |
by ikegami (Patriarch) on May 10, 2022 at 19:33 UTC | |
by Anonymous Monk on May 10, 2022 at 07:26 UTC | |
|
Re: UTF-8 and Unicode the hard way
by Anonymous Monk on May 10, 2022 at 07:21 UTC | |
by Anonymous Monk on May 10, 2022 at 17:33 UTC | |
by kcott (Archbishop) on May 11, 2022 at 09:15 UTC |