perl unicode 字符转换

[root@dr-mysql01 ~]# cat a1.pl 
my $str="$ARGV[0]";
use Encode;  
use Encode::CN;  
use JSON;   
use URI::Escape;  
use LWP::Simple;  
$str =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg;    
print "$str";


[root@dr-mysql01 ~]# perl a1.pl "\u767b\u5f55\u6210\u529f"
Wide character in print at a1.pl line 8.
登录成功You have mail in /var/spool/mail/root


需要encode_utf8:
[root@dr-mysql01 ~]# cat a1.pl 
my $str="$ARGV[0]";
use Encode;  
use Encode::CN;  
use JSON;   
use URI::Escape;  
use LWP::Simple;  
$str =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg;    
print encode_utf8($str);

[root@dr-mysql01 ~]# perl a1.pl "\u6e29\u91d1\u6240"
温金所[root@dr-mysql01 ~]# 


[root@dr-mysql01 ~]# cat hg.pl   
use Encode;  
use Encode::CN;  
use JSON;   
use URI::Escape;  
use LWP::Simple;  
open (LOG ,"<","hg.txt");  
while (){  
$content=$_;  
$content =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg;    
print "$content";  


http://download.baoxianziliao.com/tha/u4fdd/u9669/u516c/u53f8/u7231/u5fc3/u6fc0/u52b1/u4f1a/u7684/u529f/u80fd/u4e0e/u8fd0/u4f5c/u8981/u988623/u9875.ppt"

[root@dr-mysql01 ~]# cat a1.pl 
my $str="\gu4fdd\u9669\u516c\u53f8\u7231\u5fc3\u6fc0\u52b1\u4f1a\u7684\u529f\u80fd\u4e0e\u8fd0\u4f5c\u8981\u988623\u9875.ppt";
use Encode;  
use Encode::CN;  
use JSON;   
use URI::Escape;  
use LWP::Simple;  
$str =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg;    
print "$str";

[root@dr-mysql01 ~]# perl a1.pl 
gu4fdd9669516c53f872315fc36fc052b14f1a7684529f80fd4e0e8fd04f5c89819886239875.ppt[root@dr-mysql01 ~]# 


 
  
 
 

你可能感兴趣的:(zj-脚本)