Re: Another use bytes and length issue
by Joost (Canon) on Nov 06, 2006 at 19:37 UTC
|
Remember that use bytes will affect most string manipulations too, and will likely mess up the utf8 encoding flag for any strings touched in its scope.
You could introduce another scope just for the length call, but personally, I much prefer to do my $bytes = bytes::length($string);.
| [reply] [d/l] |
Re: Another use bytes and length issue
by dave_the_m (Monsignor) on Nov 06, 2006 at 20:54 UTC
|
(use bytes went beyond scope).
Huh?
$ cat /tmp/p
#!/usr/bin/perl -wl
my $s = "\x{100}";
print length $s;
{
use bytes;
print length $s;
}
print length $s;
$ perl588 /tmp/p
1
2
1
$
Dave. | [reply] [d/l] |
|
|
SCOPE: {
use bytes;
$l = length $foo;
do_something();
}
Then the scope becomes wider :) bytes will affect anything in do_something() | [reply] [d/l] |
|
|
sub print_length {
my ($s) = @_;
print(length($s), "\n");
}
my $s = "\x{100}";
print_length($s); # 1
{
use bytes;
print_length($s); # 1
print(length($s), "\n"); # 2
}
print_length($s); # 1
| [reply] [d/l] |
|
|
$s = "\x{100}";;
print do{ use bytes; length $s } . ' : ' . length $s;;
2 : 1
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
use bytes;
my $s = "\x{100}";
print length $s;
{
no bytes;
print length $s;
}
print length $s;
2
1
2
| [reply] [d/l] |
Re: Another use bytes and length issue
by graff (Chancellor) on Nov 06, 2006 at 23:29 UTC
|
Is the original data in utf8? If the data source is utf8, and you read it in as "raw bytes", the number of unicode characters will be the sum of bytes in the ascii range (x00-x7F) plus bytes greater than 0xC2 0xC0. In other words:
open( INPUT, "<:raw", "some_file.utf8" ) or die "bleah: $!";
{
local $/;
$_ = <INPUT>;
}
my $nbytes = length();
my $nchars = tr/\x00-\x7F\xC2-\xFF//;
The reason why that is true is that in utf8, every ascii character is just a single-byte ascii character, but every "wide character" (above ascii) is a sequence of two or more bytes, in which the first byte must have at least two of its highest bits set, and each successive byte comprising the one single character must have just the 8th bit set (the 7th bit must be zero) -- i.e. non-initial bytes in wide characters must always be less than 0xC0 fall between 0x80 and 0xBF, inclusive. Only one byte per character (the first one) can (and must) be greater than 0xC0.
While it is conceivable to have "wide characters" whose first byte is 0xC0 or 0xC1, this would actually constitute "malformed utf8", in the sense that two bytes are being used to express code points within the ascii range, which would be a dirty trick to play on anybody.
Check the "Unicode Encodings" section of perlunicode to see a more detailed explanation of utf8.
(updated to improve clarity and accuracy) | [reply] [d/l] |
Re: Another use bytes and length issue
by ysth (Canon) on Nov 07, 2006 at 07:02 UTC
|
I need to use "use bytes,"
I strongly doubt it. How about telling us what problem use bytes is solving for you and see if we can come up with a better way (very likely). (If you are using 5.6.x, the better way might be 5.8.x.)
| [reply] |
|
|
You guys are AWESOME! All your comments helped give me insight into this issue I've been pondering for a few months. Thanks to you guys I've found a solution! In particular, Joost mentioned that use bytes messes up utf8 encoding flags, and graff pointed me to the perl docs more utf8 byte thinking. Somehow this time this got me thinking in a different direction which led me to this: http://ahinea.com/en/tech/perl-unicode-struggle.html.
In particular, check out this:
$ustring2 = pack "U0C*", unpack "C*", $ustring2;
Whenever I did $string = chr(20000), the length function counted 1 character. When I had input from a form, the length function counted in bytes. Somehow my form input function was messing up the flags.
Apparently use bytes may have been working within scope, but the string we were analyzing had messed up utf8 flags. Putting a no bytes before and a use bytes after my few lines of counting characters keeps everything working super.
So why do we want to use bytes? Our application counts the number if bytes in a message, wraps it in a wrapper, and tells some other component how many bytes to expect. If
the count is wrong the message is rejected and components get shut down. We were uploading files with Chinese characters in the filename and the application seemed to shut down. We traced this problem to the length function counting characters instead of bytes. Since most string functions we use deal with byte counts, we'd like to use bytes and make character counts the exception.
| [reply] |
Re: Another use bytes and length issue
by Argel (Prior) on Nov 06, 2006 at 21:33 UTC
|
Have you tried 'no bytes;' after you are done with your byte processing? | [reply] |