Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 汉字不';不能总是正确显示_Perl - Fatal编程技术网

Perl 汉字不';不能总是正确显示

Perl 汉字不';不能总是正确显示,perl,Perl,我一直试图将一个汉字传递给JSON哈希,但结果总是显示为“女” 这是我得到的结果: { "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

我一直试图将一个汉字传递给JSON哈希,但结果总是显示为“女”

这是我得到的结果:

{
   "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);
但我还是得到了这样的结果: { “性别详情”:“性别” }

我最关心的是汉字(女) 被md5加密,而不是被加密的“女”

  • 如果还没有,请将源代码保存在UTF-8中
  • 告诉Perl您的脚本包含带有pragma的UTF-8:

  • 这里有一个非常简短的测试用例供您尝试:

    use strict;
    use warnings;
    use utf8;
    use JSON;
    
    print encode_json({detail_sex => '女'});
    
  • 如果还没有,请将源代码保存在UTF-8中
  • 告诉Perl您的脚本包含带有pragma的UTF-8:

  • 这里有一个非常简短的测试用例供您尝试:

    use strict;
    use warnings;
    use utf8;
    use JSON;
    
    print encode_json({detail_sex => '女'});
    
  • 如果还没有,请将源代码保存在UTF-8中
  • 告诉Perl您的脚本包含带有pragma的UTF-8:

  • 这里有一个非常简短的测试用例供您尝试:

    use strict;
    use warnings;
    use utf8;
    use JSON;
    
    print encode_json({detail_sex => '女'});
    
  • 如果还没有,请将源代码保存在UTF-8中
  • 告诉Perl您的脚本包含带有pragma的UTF-8:

  • 这里有一个非常简短的测试用例供您尝试:

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


    嗯,不完美。您确实在…警告处打印了这些
    宽字符。您应该使用pragma,也可以使用pragma在STDOUTWell上启用UTF-8编码,但不完美。您在…
    警告处打印了这些
    宽字符。您应该使用pragma,也可以使用pragma启用UTF-8编码STDOUTWell上的UTF-8编码不完美。在…
    警告中确实有那些
    宽字符。你应该使用pragma,也可以使用pragma在STDOUTWell上启用UTF-8编码,不完美。在…
    警告中确实有那些
    宽字符。你应该使用pragma,也可以使用pragma在STDOUTWell上启用UTF-8编码使用pragma在STDOUTor上启用UTF-8编码而不是数字3,用do
    JSON->new->ascii(1)
    而不是
    ->utf8(1)
    。我已经这样做了,但是我仍然得到“detail\u sex”:“å³”因此,@ysth您是对的----#3是不必要的,因为
    JSON
    模块已经编码为UTF-8。@dweston我刚刚用一个例子更新了我的答案。您能试一试吗?但是如果您通过可能不支持UTF-8的通道发送输出,添加->ascii仍然是一个好主意。它会产生
    {“详细信息”:“\u5973”}
    而不是
    {“detail\u sex”:女“}
    或者用
    JSON->new->ascii(1)
    而不是
    ->utf8(1)
    来代替数字3。我已经这样做了,但是我仍然得到了“细节性”:”的答案“因此,@ysth您是对的----#3是不必要的,因为
    JSON
    模块已经编码为UTF-8。@dweston我刚刚用一个例子更新了我的答案。您能试一试吗?但是如果您通过可能不支持UTF-8的通道发送输出,添加->ascii仍然是一个好主意。它会产生
    {“详细信息”:“\u5973”}
    而不是
    {“detail\u sex”:女“}
    或者用
    JSON->new->ascii(1)
    而不是
    ->utf8(1)
    来代替数字3。我已经这样做了,但是我仍然得到了“细节性”:”的答案“因此,@ysth您是对的----#3是不必要的,因为
    JSON
    模块已经编码为UTF-8。@dweston我刚刚用一个例子更新了我的答案。您能试一试吗?但是如果您通过可能不支持UTF-8的通道发送输出,添加->ascii仍然是一个好主意。它会产生
    {“详细信息”:“\u5973”}
    而不是
    {“detail\u sex”:女“}
    或者用
    JSON->new->ascii(1)
    而不是
    ->utf8(1)
    来代替数字3。我已经这样做了,但是我仍然得到了“细节性”:”的答案“因此,@ysth您是对的----#3是不必要的,因为
    JSON
    模块已经编码为UTF-8。@dweston我刚刚用一个例子更新了我的答案。您能试一试吗?但是如果您通过可能不支持UTF-8的通道发送输出,添加->ascii仍然是一个好主意。它会产生
    {“详细信息”:“\u5973”}
    而不是
    {“detail\u sex”:女“}