So, you have converted the original data from euc-jp to utf8, you've stored that in the database, you've fetched the utf8 data back from the database, and now you want to convert it back to euc-jp, but using the simple "encode" call for that last step was a bust. Did I get that right?

If so, the problem is probably that when you get the data back from the database, perl doesn't know that it's utf8 anymore, and the encode function will do the wrong thing as a result.

The stuff coming back from the database is a string of "octets" (which happen to constitute valid utf8 data in Japanese), and you need to convert from utf8 octets to euc-jp octets, using the "from_to" function from Encode (I'll tweak the variable names for clarity):

# prepare original data for the database: $utf8_string = decode( "euc-jp", $euc_string ); # store that to the database, then some time later, # fetch it back, and convert back to euc-jp: from_to( $db_octets, "utf8", "euc-jp" ); # now, $db_octets should be readable in an euc-jp display.
Another way to do it, depending on whether you want to do other character-based things with the stuff that comes back from the database, is to go ahead and tell perl that the database string is utf8 (by "converting" it from utf8 octets to utf8 characters, if that make sense), and then just convert it to euc-jp when you print it -- e.g. to STDOUT:
$utf8_string = decode( "utf8", $db_octets ); binmode STDOUT, ":encoding(euc-jp)"; print $utf8_string;

In reply to Re: Help with Encode::JP by graff
in thread Help with Encode::JP by GaijinPunch

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.