1
我使用Mojo :: DOM解析HTML内容。我面对的问题是程序终止,如果函数dom->find()
失败,它显示错误cannot locate object error
。我如何跳过错误并继续执行程序。请给我一些建议。跳过dom-> find()如果失败的方法
我使用Mojo :: DOM解析HTML内容。我面对的问题是程序终止,如果函数dom->find()
失败,它显示错误cannot locate object error
。我如何跳过错误并继续执行程序。请给我一些建议。跳过dom-> find()如果失败的方法
基本的方式做到这一点是:
eval{ $dom->find('arg'); };
warn "eval had returned this error : [[email protected]]\n" if [email protected];
或者与Try::Tiny:
try {
$dom->find('arg');
} catch {
warn "caught error: $_"; # not [email protected]
};
如果你知道$dom
是一个对象,但不知道是否有你想要的方法,使用can
:
if($dom->can('find')) {
# do something with $dom->find('arg');
}
该方法对于mod像URI这样的URI根据构造函数参数返回不同的子类(使用不同的方法)。
非常感谢你Sputnick :) – Balakumar