Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
Re: Re: s/.// increases length - bug or badly documented featureby Juerd (Abbot) |
on Mar 01, 2002 at 09:07 UTC ( [id://148514]=note: print w/replies, xml ) | Need Help?? |
Reducing length is normal behaviour. Of course I have example code, but I wanted someone to ask for it first. A character can be multiple bytes. Usually, when you want multi-byte characters, you use the utf8 pragma. However, multi-byte characters are already possible in strings without that pragma.
Although you can have a multi-byte character without using the pragma, the dot in the regex apparently still uses bytes (if this snipped had "use utf8", the string would be empty after the substitution). Either the regex is wrong using bytes, or length is wrong using characters. Both are documented to deal with characters, but apparently a character and a character are not the same thing. chr(12345) is 3 bytes in size, but a single character. With utf8, s/.// removes the first character, and thus all three bytes. Without utf8, s/.// removes the first _BYTE_, leaving two bytes that can't be seen as a single characters, and so making a string of two characters. Possible solutions would be:
I think perl's behaviour is buggy. Please correct me if I'm wrong. BTW - Please do read perlstyle, because a little extra white space can make your code a lot easier to read
In Section
Seekers of Perl Wisdom
|
|