siraj has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
When I am trying to decode string using utf8::decode('string') ,I am getting the following error( only in production ) environment.
'cannot decode string with wide characters'. Is there anything to do with 32 bit and 64 bit processors?
Please advice?

Replies are listed 'Best First'.
Re: Double Decoding Issue
by moritz (Cardinal) on May 24, 2010 at 19:17 UTC
    The error message tells you that what you try to decode is already decoded. I don't think 32bit vs. 64bit makes much of a difference there - more likely different perl versions, different locale, other input data etc.
    Please advice?

    Don't double-decode data.

Re: Double Decoding Issue
by graff (Chancellor) on May 25, 2010 at 03:33 UTC
    As moritz pointed out above, the issue is not 32- vs, 64-bit cpu, but rather the OS and/or perl version and/or locale setup and/or how perl was built on each machine and/or the nature of the data in your "production" vs. "other" environments, etc. (If the actual source code file happens to differ in the production vs. other environments, well, such differences are obviously of interest...)

    A quick-and-dirty work-around to make the two environments behave the same way is to make your  utf8::decode($string) call conditional on whether $string has it's "utf8 flag" set.

    The involves the is_utf8 function, which is mentioned in the utf8 manual, which in turn refers you to the more detailed description in the Encode manual, under the section "Messing with Perl's Internals" (you should take note of the caveats mentioned in both manuals):

    utf8::decode( $string ) unless ( utf8::is_utf8( $string ));
    (update: fixed missing close-paren)

    BTW, if, as suggested in the OP, you are actually passing a quoted string to  utf8::decode(); this would be quite pointless and counter-productive if your source code has  use utf8 in it, and the quoted string happens to actually be a utf8 string in the source code.

Re: Double Decoding Issue
by ikegami (Patriarch) on May 25, 2010 at 04:33 UTC
    Character decoding is the process of taking bytes and returning the characters they represent. That error indicates you asked the function to decode something that wasn't bytes.
    >perl -MEncode -we"decode('UTF-8', qq{\x{123}})" Cannot decode string with wide characters at ...

    Why your input isn't what you think it is, I don't know. You did not provide any information that would help answer that question.