perl-unicode

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

2003-07-01 04:30:05
Dan Kogai wrote:
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);

Works.

or

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

Works.

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

Still generates a warning.

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.

Thanks Dan. Your first two encode solutions work. I know it might be pushing it a bit but any ideas why? I pulled the binmode example straight from perldoc perluniintro so was surprised when it did not work except I had a suspicion that IO::Socket::INET was not returning a file handle. I added a Devel::Peek Dump of the $sock and it does not appear (to me) to be a handle:

use IO::Socket;
use Devel::Peek;
my $sock = IO::Socket::INET->new(
                                 PeerAddr => "gambantein",
                                 PeerPort => 8895,
                                 Proto => "tcp",
                                 Timeout => 20,
                                 Type => SOCK_STREAM);
if (!$sock) {
    print "Failed to connect\n";
    exit 1;
}
binmode $sock => ":encoding(utf8)";
print Dump($sock), "\n";

SV = RV(0x811e9e8) at 0x8116b30
  REFCNT = 1
  FLAGS = (PADBUSY,PADMY,ROK)
  RV = 0x826aca4
  SV = PVGV(0x8118d88) at 0x826aca4
    REFCNT = 1
    FLAGS = (OBJECT,GMG,SMG)
    IV = 0
    NV = 0
    MAGIC = 0x8118df8
      MG_VIRTUAL = &PL_vtbl_glob
      MG_TYPE = PERL_MAGIC_glob(*)
      MG_OBJ = 0x826aca4
    STASH = 0x8120310   "IO::Socket::INET"
    NAME = "GEN0"
    NAMELEN = 4
    GvSTASH = 0x8127ad4 "Symbol"
    GP = 0x8118dc0
      SV = 0x826acb0
      REFCNT = 1
      IO = 0x826ad10
      FORM = 0x0
      AV = 0x0
      HV = 0x826acec
      CV = 0x0
      CVGEN = 0x0
      GPFLAGS = 0x0
      LINE = 94
      FILE = "/usr/local/lib/perl5/5.8.0/Symbol.pm"
      FLAGS = 0x0
      EGV = 0x826aca4   "GEN0"

Thanks again.

Martin

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