in reply to Re^9: Determining content-length for an HTTP Post
in thread Determining content-length for an HTTP Post
Well, maybe it's the language barrier. My point is that it is simply not possible to encode $xmldata without knowing from what / to what.
Correct, just like you can't use use bytes; to encode strings.
If you revisit what I said, you'll notice I said he needed to encode as per the encoding specified in the <?xml?> directive. (UTF-8 is the default, btw.)
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.
When the bytes pragma is in effect, length() returns the number of bytes taken by Perl's internal string representation.
Yes, but we don't want or need that. We want the number of bytes in the string.
the only thing we really need to know for the Content-Length header is how many bytes are going to be sent. See above.
Even if I can't convince you that use bytes; is bad in general, I can clearly show that it doesn't give us the information you just said we needed.
$ perl -E' my $buf = ""; { open my $fh, ">", \$buf; utf8::upgrade( my $all_255_bytes = join "", map chr, 0..255 ); say length $all_255_bytes; say do { use bytes; length $all_255_bytes }; print $fh $all_255_bytes; } say length($buf); ' 256 length without use bytes 384 length with use bytes 256 actual content length
Given a string of bytes,
length without use bytes; always gives the number of bytes.
length with use bytes; doesn't always give the number of bytes.
Given a string of chars,
length without use bytes; always gives the number of chars.
length with use bytes; doesn't always give the number of chars.
length with use bytes; doesn't always give the bytes of the UTF-8 encoding of the chars either.
Furthermore, it is simply not true that the bytes pragma is as unreliable as you depicted it. It only fails (in this context) if you try really hard. See my examples above.
Compared to not using use bytes; which always returns the right value? Yes, it is.
I'm aware that if my advice had solved the wrong Content-Length problem, the follow-up question would probably have been: "Help! My message content is garbled!". That would have been your opportunity to shine ...
Something can be wrong and still work. Bad code sometimes works.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^11: Determining content-length for an HTTP Post
by WizardOfUz (Friar) on Nov 27, 2009 at 12:01 UTC | |
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 |