perl-unicode

Re: IO::Socket::INET and utf-8

2003-07-01 03:30:13
On Tuesday, July 1, 2003, at 05:49  PM, Martin J. Evans wrote:
Nick Ing-Simmons wrote:
Martin J. Evans <martin(_at_)easysoft(_dot_)com> writes:
A socket is a file handle so :
binmode($sock,":utf8");
should work.

This does not seem to work. My code looks like this:

    $self->{_socket} =
        IO::Socket::INET->new(
                              PeerAddr => $self->{Server},
                              PeerPort => $self->{Port},
                              Proto => "tcp",
                              Timeout => 20,
                              Type => SOCK_STREAM);
    if (!$self->{_socket}) {
        $self->{ErrorState} = "HY000";
        $self->{ErrorText} =
"Failed to connect to server $self->{Server} on port $self->{Port}";
        return undef;
    }
    $self->{_Connected} = 1;

and later I do a:

    print $self->{_socket} $string

where $string in this instance is:

my $euro = "\x{20ac}";
my $string = "insert into mjeunicode values ($euro)";

adding

    binmode($self->{_socket}, ":utf8");

after the IO::Socket::INET->new does not seem to help. Forget that this looks like it is an insert statement into a database; Perl is whinging at the point of the print to the socket.

I'm obviously missing something rather fundamental here. Any clues?

Nick's solution SHOULD BE enough but you should also try:

        use Encode;
        # ....
        print $self->{_socket} encode('utf-8' => $string);

or

        use Encode;
        # ....
        print $self->{_socket} encode_utf8($string);

or
        binmode $self->{_socket} => ":encoding(utf8)";
        # ....
        print $self->{_socket}

If you JUST want to quiet the "wide character" warning, you can also use

        no warnings 'utf8';

But I do not recommend this one.  Keep this one for the last resort.

Dan the Encode Maintainer

<Prev in Thread] Current Thread [Next in Thread>