Quoth Fabrice said:
I did read the length doc, and its not that clear.
By default length return the character length not the byte length. The
doc mention bytes::length but also says: "Note that the internal
encoding is variable, and the number of bytes usually meaningless."
Ah, I see, and I appreciate your confusion: Perl's behaviour in this
area is complicated, and not necessarily easy to understand.
pack always returns a byte string, not a character string; that is,
length and bytes::length will always give the same result, and this will
be the actual number of bytes printed.
It is not clear if the internal encoding and the result of "print" to
a file will yield the same number of bytes in all cases (eg: windows
vs unix platform, binary vs text mode files etc...)
If you are writing binary data to a file you must make sure to call
binmode on the filehandle (or open it with the :raw layer). If you have
then the same number of bytes will be printed in every case; if you try
to print a character string with characters that don't fit into a byte,
you will get a 'Wide character in print' warning. pack will never return
such a string unless you use the 'U' template.
So yes, length (or rather bytes::length) works, but I'm wondering if
this is the "proper way".
Yes, it is.
Ben