The locale environment variable affects how the length function views the string.

I don't see where this is documented, I have been unable to reproduce this, and you didn't respond to my request to demonstrate this. I have nothing to go on.

On the other hand, I am knowledgeable how Perl handles strings absent use locale;. From this point on in this post, I presume a lack of use locale;.

The UTF8 flag (and only the UTF8 flag) affects how length views a string. In the following, notice how length returns different lengths even though the variables on which they act have the same internal representation ("\303\251")? The difference is that the first has the UTF8 flag on.

$ perl -MDevel::Peek -le'utf8::upgrade(my $x = chr(0xE9)); print lengt +h($x); Dump($x)' 1 SV = PVMG(0x817fd40) at 0x814cc6c REFCNT = 1 FLAGS = (PADBUSY,PADMY,SMG,POK,pPOK,UTF8) IV = 0 NV = 0 PV = 0x815bda0 "\303\251"\0 [UTF8 "\x{e9}"] CUR = 2 LEN = 3 MAGIC = 0x8163060 MG_VIRTUAL = &PL_vtbl_utf8 MG_TYPE = PERL_MAGIC_utf8(w) MG_LEN = 1 $ perl -MDevel::Peek -le'utf8::encode(my $x = chr(0xE9)); print length +($x); Dump($x)' 2 SV = PV(0x814ce90) at 0x814cc6c REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x815bda0 "\303\251"\0 CUR = 2 LEN = 3

The string is represented internally as UTF8 (UTF8 flag on) when bytes are decoded into characters. This can be done on scalars (utf8::decode or Encode::decode) or on file handles (using the :encoding PerlIO layer).


In reply to Re^3: setlocale not working properly in perl script by ikegami
in thread 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.