如何将 autodie 与非内置函数一起使用?
autodie 文档暗示,除了默认情况下可以处理的内置函数之外,还可以将它用于其他功能,但没有明确的示例如何在其中执行此操作。
具体来说,我想将它用于成像器模块。其中的许多函数和方法都可能会失败,我希望这不会意味着我的代码将到处都是 或 die Imager|$image->errstr;
短语。
当然,如果除了使用 autodie 之外还有其他方法来实现这一点,我也会对此感兴趣。
The autodie documentation hints that it is possible to use it for other functions than those built-ins which it can handle by default, but there are no clear examples how to do that in it.
Specifically I would like to use it for the Imager module. A lot of the functions and methods of that can fail, and I would prefer if that wouldn't mean that my code will be littered with or die Imager|$image->errstr;
phrases all over.
Of course, if there's another way than using autodie to achieve that, I would be interested in that too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
autodie 仅适用于函数,不适用于方法。这是因为它是词法作用域的,而方法查找不能是词法作用域的。 autodie::hints 解释了如何告诉 autodie 有关用户定义函数的信息,但是不会对方法做任何事情。
我不知道有什么方法可以让方法获得类似自动死亡的行为,除非模块内置了该行为(例如 DBI 的
RaiseError
)。您可以使用一个子例程来执行检查,但它不会保存那么多代码,因为您仍然必须向其传递正确的对象或类来调用
errstr
。autodie only works with functions, not methods. This is because it's lexically scoped, and method lookup can't be lexically scoped. autodie::hints explains how to tell autodie about user-defined functions, but that won't do anything for methods.
I don't know of any way to get autodie-like behavior for methods, unless the module has that built in (e.g. DBI's
RaiseError
).You could have a subroutine to do the check, but it wouldn't save all that much code, since you'd still have to pass it the correct object or class to call
errstr
on.请参阅 autodie::hints
See autodie::hints
这是一种与方法一起使用的替代技术:
并使用它:
Here is an alternative technique that works with methods:
And to use it: