in reply to Re: Portable length() in bytes.
in thread Portable length() in bytes.
Well, think about it for a moment. What if your scalar contains arbitrary binary data like a JPEG or *.tar.gz file? You don't want the length in "characters" for it because random byte sequences could get mistaken for multi-byte UTF-8 characters, resulting in a shorter length() than you expected.
A better example is UTF-8 itself. Tell me, how would you send a UTF-8 string with multi-byte UTF-8 characters in it over a network? In addition, how could you do it portably, so your code would work back to version 5.005 of Perl?
# five UTF smiley faces (three bytes long each): my $string = "\x{263a}\x{263a}\x{263a}\x{263a}\x{263a}"; my $bytes_written = syswrite($socket, $string, length $string);
Oops. That ends up writing only five bytes to the socket instead of fifteen, because length() returns the length in characters, not bytes, and each of those smiley faces takes up three bytes.
Use size_in_bytes() instead and it works regardless of what Perl you're using:
# five UTF smiley faces (three bytes long each): my $string = "\x{263a}\x{263a}\x{263a}\x{263a}\x{263a}"; my $bytes_written = syswrite($socket, $string, size_in_bytes($string)) +;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Portable length() in bytes.
by ysth (Canon) on Nov 07, 2004 at 21:55 UTC | |
by William G. Davis (Friar) on Nov 07, 2004 at 23:19 UTC | |
by ysth (Canon) on Nov 07, 2004 at 23:34 UTC | |
by William G. Davis (Friar) on Nov 08, 2004 at 00:37 UTC | |
by ysth (Canon) on Nov 08, 2004 at 05:58 UTC | |
|