in reply to Re^2: Size and anatomy of an HTTP response
in thread Size and anatomy of an HTTP response
More precisely length() always returns what it thinks are the number of characters in the string. This "thinking" relies on the value of the utf8 flag. The reply you linked to refered to a "unicode string", i.e. one with its unicode flag set.
If the utf8 flag is set, it assumes each byte is an octet and glues octets together into single characters as needed, so you might have bytes = characters or not. If the utf8 flag is NOT set, then it counts pure bytes on the assumption that there is a one-to-one relationship between bytes and characters. In that case there is no difference between the byte count and the character count. If your utf8 octets are all in the ascii range you will never notice the difference and byte count will equal character count, but if for some reason you have a string full of utf8 octets and the utf8 flag gets switched off (perhaps you opened a stream raw mode but the file was filled with non-ascii utf8 octets?), length will return the number of bytes NOT the number of characters.
Here is a quick example of the difference a flag makes. Nothing has changed in the content of $s. Only the utf8 bit has been changed, and presto the length goes from 1 to 2.
use Encode; my $s=chr(0x0160); printf "chr=<%s> utf8-flag=%s length=%d\n" , $s, Encode::is_utf8($s)?'yes':'no', length($s); #outputs: chr=<?> utf8-flag=yes length=1 Encode::_utf8_off($s); printf "chr=<%s> utf8-flag=%s length=%d\n" , $s, Encode::is_utf8($s)?'yes':'no', length($s); #outputs: chr=<?> utf8-flag=no length=2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Size and anatomy of an HTTP response
by Discipulus (Canon) on Dec 16, 2010 at 09:47 UTC | |
by Anonymous Monk on Dec 16, 2010 at 10:13 UTC | |
by afoken (Chancellor) on Dec 16, 2010 at 14:06 UTC | |
by ELISHEVA (Prior) on Dec 16, 2010 at 10:34 UTC | |
by Discipulus (Canon) on Dec 16, 2010 at 12:49 UTC |