汉字并不总是正确显示

时间:2015-09-03 21:19:34

标签: perl

我一直试图将中文字符传递给JSON哈希,但它总是以“女”

的形式出现
#!/usr/bin/perl

use JSON;
# variable declaration
my $gender = "Female"
# turning english selection to Chinese character
if ($gender eq 'Female') {
    $gender = "女";
} elsif ($gender eq 'Male') {
    $gender = "男";
} elsif ($gender eq 'Decline to state') {
    $gender = "";
}

my $hash_ref = {};

$hash_ref->{'detail_sex'} = $gender;

print JSON->new->utf8(1)->pretty(1)->encode($hash_ref);`

这是我得到的结果:

{
   "detail_sex" : "女"
}

然而,当我测试另一个脚本时,汉字完美地出现了。

#!/usr/bin/perl
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Encode qw(encode_utf8);
use JSON;

my $userid  = 1616589;
my $time    = 2015811;
my $ejob_id = 1908063;

# md5 encryption without chinese characters
my $md5_hex_sign = md5_hex($userid,$time,$job_id);
print "$md5_hex_sign\n";

# seeing if character will print
print "let's try encoding and decrypting \n"; 
print "the character to encrypt.\n";
print "女\n";
print "unicode print out\n";
print "\x{5973}\n";
my $char = "\x{5973}";
my $sign_char = "女";
print "unicode stored in \$char variable \n";
print $char, "\n";
print "md5 encryption of said chinese character from \$char with utf8 encoding\n";
print md5_hex(encode_utf8($char)), "\n";
print "md5 encryption of wide character with utf8 encoding\n";
print md5_hex(encode_utf8("女")), "\n";
my $sign_gender = md5_hex(encode_utf8($sign_char));
#JSON

print "JSON print out\n";

my $hash_ref = {};

$hash_ref->{'gender'} = $char;
$hash_ref->{'md5_gender'} = md5_hex(encode_utf8($char));
$hash_ref->{'char_gender'} = md5_hex(encode_utf8("女"));
$hash_ref->{'sign_gender'} = $sign_gender;
print JSON->new->utf8(1)->pretty(1)->encode($hash_ref);`

结果如下:

160a6f4bf9aec1c2d102330716ca8f4e
let's try encoding and decrypting
the character to encrypt.
女
unicode print out
Wide character in print at md5check.pl line 18.
女
unicode stored in $char variable
Wide character in print at md5check.pl line 22.
女
md5 encryption of said chinese character from $char with utf8 encoding
87c835a6b1749374a7524a596087b296
md5 encryption of wide character with utf8 encoding
06c82a10da7e297180d696ed92f524c1
JSON print out
{
   "char_gender" : "06c82a10da7e297180d696ed92f524c1",
   "md5_gender" : "87c835a6b1749374a7524a596087b296",
   "sign_gender" : "06c82a10da7e297180d696ed92f524c1",
   "gender" : "女"
}

有人会向我解释发生了什么事吗?

我尝试过的事情:

use utf8;
print JSON->new->ascii(1)->pretty(1)->encode($hash_ref);

但我仍然得到这个结果: {        “detail_sex”:“女” }

我最关心的是中文字符(女)被加密而不是加密的“女”。

1 个答案:

答案 0 :(得分:3)

  1. 如果还没有,请将您的源代码保存为UTF-8。
  2. 告诉Perl您的脚本包含带有utf8编译指示的UTF-8:

    use utf8;
    
  3. 这是一个非常简短的测试案例供您尝试:

    use strict;
    use warnings;
    use utf8;
    use JSON;
    
    print encode_json({detail_sex => '女'});