如何在Perl中处理UTF16警告机械化调用

问题描述:

我在使用perl中的机械化对使用utf16字符的网站进行机械化调用时出错。它告诉我这个警告Parsing of undecoded UTF-16 at /usr/local/share/perl5/LWP/UserAgent.pm line 600我知道这是在我调用$ mech-> content()方法时生成的。有没有办法在机械化的内容方法中忽略这些警告?如何在Perl中处理UTF16警告机械化调用

是的,你可以忽略警告这样的:

{ 
    no warnings; 
    #your code that generate false warnings 

}; 

你可以用这个解决编码错误,它可能工作。

WWW :: Mechanize是LWP :: UserAgent的适当子类,您也可以使用LWP :: UserAgent的任何方法。

my $content = $mech->decoded_content();# 
if (utf8::is_utf8($content)) { 
    binmode STDOUT,':utf8'; 
} else { 
    binmode STDOUT,':raw'; 
} 
print $content; 
+0

第二种方法适用于我。忽略警告会影响我需要警告的其他功能。 – 2013-02-13 09:05:28

+1

这种方式的忽略警告本地化为闭包。您可以通过这种方式忽略仅一条语句的警告。 – user1126070 2013-02-13 09:43:24

+1

你的意思是“curlies”或“block”。 “关闭”是非常不同的东西。 – ikegami 2013-02-14 06:03:53