通过demo搞懂encode_utf8和decode_utf8

时间:2021-12-01 19:54:42
perl 编码问题;

use utf8表示代码使用UTF-8写的

[root@wx03 0710]# cat a1.pl
#use utf8;
my $str='测试utf8字符集';
print $str."\n";
[root@wx03 0710]# perl a1.pl
测试utf8字符集 [root@wx03 0710]# cat a1.pl
use utf8;
my $str='测试utf8字符集';
print $str."\n";
[root@wx03 0710]# perl a1.pl
Wide character in print at a1.pl line 3.
测试utf8字符集 [root@dr-mysql01 0710]# cat a1.pl
use utf8;
my $a='验证';
print $a."\n"; my $b=$a;
#my $b=encode_utf8($a); my $url="http://192.168.32.101:3000/api/getcode?env=zj&phone=18072722237";
use LWP::UserAgent;
use Encode;
#use utf8; my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0"); my $response = $ua->get($url); if ($response->is_success) {
$r= $response->decoded_content; # or whatever
print "\$r is $r\n";
}
else {
die $response->status_line;
}; if ($r =~/$b/){print "11111111111\n"};
[root@dr-mysql01 0710]# perl a1.pl
Wide character in print at a1.pl line 3.
验证
$r is [["18072722237","ZJ没有找到需要的验证码信息"]] 此时没有匹配到 [root@dr-mysql01 0710]# cat a1.pl
use utf8;
my $a='验证';
print $a."\n"; #my $b=$a;
my $b=encode_utf8($a); my $url="http://192.168.32.101:3000/api/getcode?env=zj&phone=18072722237";
use LWP::UserAgent;
use Encode;
#use utf8; my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0"); my $response = $ua->get($url); if ($response->is_success) {
$r= $response->decoded_content; # or whatever
print "\$r is $r\n";
}
else {
die $response->status_line;
}; if ($r =~/$b/){print "11111111111\n"};
[root@dr-mysql01 0710]# perl a1.pl
Wide character in print at a1.pl line 3.
验证
$r is [["18072722237","ZJ没有找到需要的验证码信息"]]
11111111111 此时匹配到了 在看看mojo里的 默认使用utf8; 此时返回空:[root@wx03 ~]# curl http://120.55.118.6:3000/admin/api/menu
[][root@wx03 ~]# [root@wx03 mojo]# cat test.pl
use Mojolicious::Lite;
use JSON qw/encode_json decode_json/;
use Encode;
no strict;
use JSON;
use Data::Dumper;
# /foo?user=sri
get '/admin/api/menu' => sub {
my $c = shift;
print "测试更健康\n";
open (LOG1 ,"<",'/data01/applog_backup/zj-api01-catalina.out') or die $!;
while (<LOG1>) {
my $phone='18072722237';
#2016-03-09 09:35:12,380 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为132435,本验证码5分钟内有效。 to phoneNo:18072722237 succeed! if ( ($phone) and ($_ =~/$phone/) and ($_ =~/SMSUtils/) ){$str=$_; # my $d=encode_utf8('验证');
my $d='验证'; print "1-------\$str is $str\n";
next if ($str !~ /$d/);
print "2--------\$str is $str\n";
push (@arr1 ,decode_utf8($str));
}}; close LOG1;
$c->render(json => \@arr1 );
}; app->start; /************************************************************************************
[root@wx03 mojo]# cat test.pl
use Mojolicious::Lite;
use JSON qw/encode_json decode_json/;
use Encode;
no strict;
use JSON;
use Data::Dumper;
# /foo?user=sri
get '/admin/api/menu' => sub {
my $c = shift;
print "测试更健康\n";
open (LOG1 ,"<",'/data01/applog_backup/zj-api01-catalina.out') or die $!;
while (<LOG1>) {
my $phone='18072722237';
#2016-03-09 09:35:12,380 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为132435,本验证码5分钟内有效。 to phoneNo:18072722237 succeed! if ( ($phone) and ($_ =~/$phone/) and ($_ =~/SMSUtils/) ){$str=$_; my $d=encode_utf8('验证');
# my $d='验证'; print "1-------\$str is $str\n";
next if ($str !~ /$d/);
print "2--------\$str is $str\n";
#push (@arr1 ,decode_utf8($str));
push (@arr1 ,$str);
}}; close LOG1;
$c->render(json => \@arr1 );
}; app->start; 此时返回乱码:
[root@wx03 ~]# curl http://120.55.118.6:3000/admin/api/menu
["2016-04-22 11:02:01,259 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为813231,本验证码30分钟内有效。 to phoneNo:18072722237 succeed!\n","2016-04-22 11:02:01,259 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为813231,本验证码30分钟内有效。 to phoneNo:18072722237 succeed!\n"][root@wx03 ~]# /************************************************************************************
[root@wx03 mojo]# cat test.pl
use Mojolicious::Lite;
use JSON qw/encode_json decode_json/;
use Encode;
no strict;
use JSON;
use Data::Dumper;
# /foo?user=sri
get '/admin/api/menu' => sub {
my $c = shift;
print "测试更健康\n";
open (LOG1 ,"<",'/data01/applog_backup/zj-api01-catalina.out') or die $!;
while (<LOG1>) {
my $phone='18072722237';
#2016-03-09 09:35:12,380 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为132435,本验证码5分钟内有效。 to phoneNo:18072722237 succeed! if ( ($phone) and ($_ =~/$phone/) and ($_ =~/SMSUtils/) ){$str=$_; my $d=encode_utf8('验证');
# my $d='验证'; print "1-------\$str is $str\n";
next if ($str !~ /$d/);
print "2--------\$str is $str\n";
push (@arr1 ,decode_utf8($str));
}}; close LOG1;
$c->render(json => \@arr1 );
}; app->start; 此时返回: ][root@wx03 ~]# curl http://120.55.118.6:3000/admin/api/menu
["2016-04-22 11:02:01,259 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为813231,本验证码30分钟内有效。 to phoneNo:18072722237 succeed!\n","2016-04-22 11:02:01,259 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为813231,本验证码30分钟内有效。 to phoneNo:18072722237 succeed!\n"][root@wx03 ~]#