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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |