Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove wide characters, Perl

Tags:

unicode

perl

I am trying to send a String via a socket using a perl program. I am getting an error saying that the text contains a wide character, and the socket can't deal with that. Is there a way to either:

A: Turn on wide characters through the socket

or

B: Remove all wide characters from a string?

like image 364
providence Avatar asked Oct 07 '11 06:10

providence


1 Answers

It means you're trying to send text over a handle, yet handles can only communicate bytes. You need to serialise the text into bytes. Specifically, you want to encode the text. You can use Encode's encode function

print $sock encode('some_encoding', $text);

or you can instruct the socket to do it for you

binmode $sock, ':encoding(some_encoding)';  # once
print $sock $text;

Replace some_encoding with the encoding expected by the other end of the socket (e.g. UTF-8).

like image 104
ikegami Avatar answered Sep 21 '22 20:09

ikegami