我正在使用 Mojo::DOM 来解析 HTML 内容。我面临的问题是如果函数dom->find()
失败,程序会终止,它会显示错误cannot locate object error
。我怎样才能跳过错误并继续执行程序。请给我一些建议。
问问题
63 次
2 回答
2
这样做的基本方法是:
eval{ $dom->find('arg'); };
warn "eval had returned this error : [$@]\n" if $@;
或使用Try::Tiny:
try {
$dom->find('arg');
} catch {
warn "caught error: $_"; # not $@
};
于 2013-08-31T20:10:33.897 回答
0
如果您知道$dom
是一个对象但不知道它是否具有您想要的方法,请使用can
:
if($dom->can('find')) {
# do something with $dom->find('arg');
}
这种方法对于像 URI 这样的模块很有用,它根据构造函数参数返回不同的子类(使用不同的方法)。
于 2013-09-01T03:45:40.430 回答