in reply to Re^10: Determining content-length for an HTTP Post
in thread Determining content-length for an HTTP Post
Given a string of bytes,
...
length with use bytes; doesn't always give the number of bytes.
You are spreading (dangerous) FUD, no offence meant (really!). The bytes pragma works as advertised. The problem with your example is that you are fiddling with the internal UTF-8 flag. The perlunicode document clearly calls utf8::upgrade() a "low-level" function. The right way to UTF-8-encode a string is utf8::encode() or, maybe even better, the Encode module. See:
joerg@Marvin:~> perl -E' my $buf = ""; { open my $fh, ">", \$buf; utf8::encode( my $all_256_bytes = join "", map chr, 0..255 ); say length $all_256_bytes; say do { use bytes; length $all_256_bytes }; print $fh $all_256_bytes; } say length($buf); ' 384 384 384
Btw, I have already demonstrated this in my previous post. You might want to reread it; example [6] is especially interesting.
Therefore, the only (quick) advice I could give him was to make sure that length() treats $xmldata as a series of bytes.use bytes; does no such thing.
This is taken right from the "bytes" documentation:
"Perl normally assumes character semantics in the presence of character data (i.e. data that has come from a source that has been marked as being of a particular character encoding). When use bytes is in effect, the encoding is temporarily ignored, and each string is treated as a series of bytes."
Maybe you should rethink your statement.
We want the number of bytes in the string.
No, we (only) need to know how many bytes are going to be sent. Please reread my previous post.
Something can be wrong and still work. Bad code sometimes works.
Agreed. But, given the limited amount of information we have, my suggestion is still the best first step to take in solving the OP's problem.
Peace.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^12: Determining content-length for an HTTP Post
by ikegami (Patriarch) on Nov 27, 2009 at 16:22 UTC | |
by WizardOfUz (Friar) on Nov 27, 2009 at 18:05 UTC | |
by Corion (Patriarch) on Nov 27, 2009 at 18:11 UTC |