Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Thank you very much, moritz, for your helpful reply. I greatly appreciate it.

So, what did you try?

I tried variations of something very similar to your second example using Encode::Repair::learn_recoding(). In hindsight, I believe the problem that thwarted my efforts was my inclusion of use utf8 in the script. Needless to say, I thought I was doing the right thing when, in fact, I was doing exactly the wrong thing.

I just tested using Encode::Repair to repair damaged text that has non-ASCII characters in it.

    敌馀⁳潧琠韦겜鯥↽
    \u654C\uE274\u9980\u2073\u6F67\u7420\u206F\u97E6\uE6A5\uAC9C\u9BE5\u21BD
    \x4C\x65\x74\xE2\x80\x99\x73\x20\x67\x6F\x20\x74\x6F\x20\xE6\x97\xA5\xE6\x9C\xAC\xE5\x9B\xBD\x21
    \u004C\u0065\u0074\u2019\u0073\u0020\u0067\u006F\u0020\u0074\u006F\u0020\u65E5\u672C\u56FD\u0021
    Letís go to 日本国!

It works! Here's the script.

use v5.16;

use Encode::Repair qw( repair_encoding );

while (my $damaged_text = <DATA>) {
    chomp $damaged_text;

    my $repaired_text = repair_encoding(
        $damaged_text, [
            decode => 'UTF-8',
            encode => 'UCS-2LE',
        ]
    );

    say $repaired_text;
}

exit 0;

__DATA__
敒õ剕䕇呎
敌馀⁳潧琠韦겜鯥↽

Brilliantly, this prints…

    Re: URGENT
    Letís go to 日本国!

Notice, however, that I had to remove binmode STDOUT, ':encoding(UTF-8)'. This version of the script also works.

use v5.16;

use Encode qw( decode );
use Encode::Repair qw( repair_encoding );

binmode STDOUT, ':encoding(UTF-8)';

while (my $damaged_text = <DATA>) {
    chomp $damaged_text;

    my $repaired_text = repair_encoding(
        $damaged_text, [
            decode => 'UTF-8',
            encode => 'UCS-2LE',
        ]
    );

    say decode('UTF-8', $repaired_text);
}

exit 0;

__DATA__
敒õ剕䕇呎
敌馀⁳潧琠韦겜鯥↽

I can study and study and study the Perl documentation, but I'll never grok the subtleties of Perl's Unicode model. It's simply too profoundly confusing for me.

I love your module, moritz! Thanks for it, and for your help here.


In reply to Re^2: How to Fix Character Encoding Damaged Text Using Perl? by Jim
in thread How to Fix Character Encoding Damaged Text Using Perl? by Jim

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (14)
    As of 2014-07-25 20:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (175 votes), past polls