perl 自动识别编码,转换编码

use Encode;
use File::Copy;
use File::Copy::Recursive qw(rmove);
my $f=$ARGV[0];
my $a=`file --mime-encoding "$f"`;
my @b= split /:/,$a;
my $c=$b[1];
$c =~ s/\s//g;
print $c;
print "\n";
if ($c eq 'utf-8' ){
  print "$f=$c"."\n";
}
elsif($c eq 'iso-8859-1'){
  print "$f=$c"."\n";
  open (FILE ,"<","$f");  
  open(OUTFILE, ">","$f.1");
  while (<FILE>) {  
    $str = encode("utf-8",decode("GB2312","$_"));
    print OUTFILE ($str);
  };
  close FILE;
  close OUTFILE;
  rmove ("$f.1", "$f");
}
else
 {print "not support "."$f=$c"."\n";};

猜你喜欢

转载自blog.csdn.net/zhaoyangjian724/article/details/86612922