I've encountered an issue in my perl script where the locale of the box I'm running on affects the length() calculation of strings with multibyte characters. The current locale setting of the box is en_US.UTF-8. If I "export LC_CTYPE=en_US" at the UNIX prompt and then run the script, the length() function does what I want it to. However, I want the script to set the locale dynamically. I found the following code from a site online but none of this seems to have the effect that it should. For instance I use:
use locale; use POSIX qw(locale_h); $oldlocale = setlocale(LC_CTYPE); $newlocale = setlocale(LC_CTYPE, "en_US");
The setlocale function seems to set the locale correctly as when I print $oldlocale and $newlocale I get en_US.UTF-8 and en_US respectively.

However my length function still treats the multibyte characters in an undesired way.

How come when I change the locale at the UNIX prompt it works as desired but when I change it in the script, it does not?

Thanks,
Steve

In reply to setlocale not working properly in perl script by TowerGuard

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.