perl socket传hash(use Storable)
cpan关于Storable的例子 use Storable qw(store retrieve freeze thaw dclone); %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1); store(%color, 'mycolors') or die "Can't store %a in mycolors!n"; $colref = retrieve('mycolors'); die "Unable to retrieve from mycolors!n" unless defined $colref; printf "Blue is still %lfn", $colref->{'Blue'}; $colref2 = dclone(%color); $str = freeze(%color); printf "Serialization of %%color is %d bytes long.n", length($str); $colref3 = thaw($str); 用在socket上 client: #!/usr/bin/perl my $lsocket=&nsock; my %color = ('Blue' => 0.1,'Red' => 0.8,'Black' => 0,'White' => 1); print $lsocket $str; $lsocket->shutdown(1); sub nsock(){ ? ? ----- server端 #!/usr/bin/perl my $lsocket=IO::Socket::INET->new( 结果: [root@localhost socket]# perl server.pl ?linux-windows socket传中文字符会出现乱码 今天使用storable又发现一个问题,不知道是不是我哪里出错了 问题描述 my %color = ('Blue' => ‘aa’,'White' => 1); 传这样的hash socket另一端解析不了。 建议使用JSON模块,很简单,还可以处理中文字符问题 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |