If that's any comfort, you are probably not the only one to have been tortured by Unicode for that long. I have a last-resort shell trick that goes  iconv -f utf8 -t utf8 <dodgy_file> || iconv -f iso-8859-1 -t utf8 <dodgy_file> > <utf8_file> for those cases where I am not 100% sure of the encoding I get, and I don't have the time to debug it properly.

In your case I wonder if the problem is not that you have some non Latin1 characters in your input (that got there through entities in the HTML maybe, something like &mdash; for example.

My machine has got UTF-8 locale, so I can't reproduce exactly your problem, but the 2 one liners below give me outputs in different encodings:

perl -MHTML::TreeBuilder::XPath -e'$t= HTML::TreeBuilder::XPath->new; $t->parse( "<html><body><p>para &copy;</p></body></html>"); $p=$t->findvalue( "//p"); print $p, "\n";'

That first one gives me a result in latin1.

perl -MHTML::TreeBuilder::XPath -e'$t= HTML::TreeBuilder::XPath->new; $t->parse( "<html><body><p>para &copy; &mdash;</p></body></html>"); @p=$t->findnodes( "//p"); print $p[0]->as_text(), "\n";'

I just added an &mdash;, an entity that cannot be printed in Latin1, to the input et voilą! Now the result is in UTF-8.

In that case using HTML::Entities should help:

perl -MHTML::TreeBuilder::XPath -MHTML::Entities -e' $t= HTML::TreeBuilder::XPath->new; $t->parse( "<html><body><p>para &copy; &mdash;</p></body></html>"); @p=$t->findnodes( "//p"); $out= $p[0]->as_text; encode_entities( $out); print $out, "\n";'

In reply to Re: Unicode nightmare: is there a cheat sheet or solutions diagram? by mirod
in thread Unicode nightmare: is there a cheat sheet or solutions diagram? by rduke15

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.