I am implementing a network client that sends messages to the server. Messages are streams of bytes, and the protocol requires me to send the length of each stream in advance.
If the message I give (with code using my module) is a byte string, then the length is given quite easily with length $string. But if it is a character string, I will need to massage it to get the raw bytes. What I'm doing now is basically this:
my $msg = shift;
my $bytes;
if ( utf8::is_utf8( $msg ) ) {
$bytes = Encode::encode( 'utf-8', $msg );
} else {
$bytes = $msg;
}
my $length = length $bytes;
Is this the right way to handle this? It seems to be working so far, but I haven't done any serious testing yet. What potential traps exist with this approach?
thank