<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
try this?<br>
<br>
<big><i><b>use Encode qw(encode decode);<br>
<br>
sub is_utf8{<br>
    my $a = shift;<br>
    my $b = encode('utf8', decode('utf8', $a));<br>
    <br>
    return ($a eq $b) ? 1 : 0;<br>
}<br>
<br>
sub is_gbk{<br>
    my $a = shift;<br>
    my $b = encode('gbk', decode('gbk', $a));<br>
    <br>
    return ($a eq $b) ? 1 : 0;<br>
}</b></i></big><br>
<br>
silent wrote:
<blockquote cite="mid:492E4203.7050300@gmail.com" type="cite">
  <pre wrap="">了解, 可是我的那两个函数为什么不对呢?

agentzh 写道:
  </pre>
  <blockquote type="cite">
    <pre wrap="">我们一般使用 CPAN 上的 Encode::Guess 模块。对于长文本非常有效的,但是对于非常短的,比如两三个字的文本就不怎么准了,呵呵。

       use Encode::Guess;
       my @enc = qw( UTF-8 GB2312 Big5 GBK Latin1 );
       for my $enc (@enc) {
           my $decoder = guess_encoding($data, $enc);
           if (ref $decoder) {
               $charset = $decoder-&gt;name;
               last;
           }
       }
       if (!$charset) {
           die "Can't determine the charset of the input.\n";
       }

这里 @enc 中是尝试的 charset 数量。其实感觉用 Encode 的 decode
函数也可以直接整,只不过设一个参数让它遇到错误字节时直接抛异常即可 ;)

-agentzh
  
    </pre>
  </blockquote>
  <pre wrap=""><!---->
_______________________________________________
China-pm mailing list
<a class="moz-txt-link-abbreviated" href="mailto:China-pm@pm.org">China-pm@pm.org</a>
<a class="moz-txt-link-freetext" href="http://mail.pm.org/mailman/listinfo/china-pm">http://mail.pm.org/mailman/listinfo/china-pm</a></pre>
</blockquote>
<br>
</body>
</html>