代码之家  ›  专栏  ›  技术社区  ›  Karel Bílek

“无法解码宽字符字符串”出现在奇怪的地方

  •  5
  • Karel Bílek  · 技术社区  · 17 年前

    use HTTP::Request;
    use LWP::UserAgent;
    use XML::RAI;
    use Encode;
    
    my $ua = LWP::UserAgent->new;
    
    
    sub readFromWeb{
        my $address = shift;
        my $request = HTTP::Request->new( GET => $address );
        my $response = $ua->request( $request );
        return unless $response->code == 200;
    
        return decode("utf8", $response->content());
    }
    
    sub readFromRSS{
        my $address=shift;
        my $content = readFromWeb $address;
        my $rai = XML::RAI->parse_string($content);
              #this line "causes" the error
    }
    
    
    readFromRSS("http://aktualne.centrum.cz/export/rss-hp.phtml");
         #I am testing it on this particular RSS
    

    错误是:

     Cannot decode string with wide characters at /usr/lib/perl5/5.8.8/i686-linux/Encode.pm line 166.
    

    1 回复  |  直到 17 年前
        1
  •  9
  •   trendels    17 年前

    XML::RAI::parse_string()

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Encode qw( decode );
    use LWP::Simple qw( get );
    
    my $xml = get("http://aktualne.centrum.cz/export/rss-hp.phtml");
    
    $xml = decode('UTF-8', $xml);
    $xml = decode('UTF-8', $xml); # dies: Cannot decode string with wide characters ...
    

    decode()