Perl:UTF8 字符在传输时失真
Perl : UTF8 characters gets distorted while trasnmitting
我有一个脚本可以将 HL7 消息发送给 Mirth。这是程序:
use Net::HL7;
use Net::HL7::Connection;
use open ( ":encoding(UTF-8)", ":std" );
binmode(STDOUT, ":utf8");
my $conn = new Net::HL7::Connection('127.0.0.1', 7010);
my $msg = getHl7Message(); # Too many things happening in getHl7Message()
print($msg) # All characters are correct in $msg when printed
my $hl7msg = new Net::HL7::Message($msg);
print($hl7msg->toString(1)) #All characters are correct
my $response = $conn->send($hl7msg); #sent to Mirth
现在,当我检查 mirth 时,ASCII 集之外的所有字符都被扭曲了。
我该怎么办? Net::HL7::Connection 在内部使用 IO::Socket。
我也收到了这个警告:
Wide character in print at /usr/local/share/perl5/Net/HL7/Connection.pm line 143.
我尝试使用 -CS 执行但仍然没有收获。
一些信息:
[user@server gs]$ lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch
Distributor ID: OracleServer
Description: Oracle Linux Server release 6.6
Release: 6.6
Codename: n/a
/etc/environment 和 /etc/default/locale 是空的。我添加了这两行
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
结果:
[user@server gs]$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
您将 UTF-8 编码层放在 STDOUT 上,但您没有将该层放在 HL7 通道(无论是什么)上,也没有用 UTF-8 编码您的消息。这就是您看到 Wide print ...
警告的原因。将 UTF-8 编码的字符串写入 HL7 连接。
my $msg = getHl7Message();
# $msg is not encoded.
# map{chr}split//,$msg should produce some values larger than 255
print($msg);
# this is ok because the :utf8 layer was applied to STDOUT.
# Behind the scenes, the string $msg is encoded before it is output
# to the terminal.
my $msgutf8 = Encode::encode("UTF-8", $msg);
# Now $msgutf8 is a UTF-8 encoded "octet string".
# map{chr}split//,$msgutf8 should only produce vals between 0 and 255,
# and is safe to transmit
my $hl7msg = new Net::HL7::Message($msgutf8);
我有一个脚本可以将 HL7 消息发送给 Mirth。这是程序:
use Net::HL7;
use Net::HL7::Connection;
use open ( ":encoding(UTF-8)", ":std" );
binmode(STDOUT, ":utf8");
my $conn = new Net::HL7::Connection('127.0.0.1', 7010);
my $msg = getHl7Message(); # Too many things happening in getHl7Message()
print($msg) # All characters are correct in $msg when printed
my $hl7msg = new Net::HL7::Message($msg);
print($hl7msg->toString(1)) #All characters are correct
my $response = $conn->send($hl7msg); #sent to Mirth
现在,当我检查 mirth 时,ASCII 集之外的所有字符都被扭曲了。
我该怎么办? Net::HL7::Connection 在内部使用 IO::Socket。
我也收到了这个警告:
Wide character in print at /usr/local/share/perl5/Net/HL7/Connection.pm line 143.
我尝试使用 -CS 执行但仍然没有收获。
一些信息:
[user@server gs]$ lsb_release -a
LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch
Distributor ID: OracleServer
Description: Oracle Linux Server release 6.6
Release: 6.6
Codename: n/a
/etc/environment 和 /etc/default/locale 是空的。我添加了这两行
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
结果:
[user@server gs]$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
您将 UTF-8 编码层放在 STDOUT 上,但您没有将该层放在 HL7 通道(无论是什么)上,也没有用 UTF-8 编码您的消息。这就是您看到 Wide print ...
警告的原因。将 UTF-8 编码的字符串写入 HL7 连接。
my $msg = getHl7Message();
# $msg is not encoded.
# map{chr}split//,$msg should produce some values larger than 255
print($msg);
# this is ok because the :utf8 layer was applied to STDOUT.
# Behind the scenes, the string $msg is encoded before it is output
# to the terminal.
my $msgutf8 = Encode::encode("UTF-8", $msg);
# Now $msgutf8 is a UTF-8 encoded "octet string".
# map{chr}split//,$msgutf8 should only produce vals between 0 and 255,
# and is safe to transmit
my $hl7msg = new Net::HL7::Message($msgutf8);