如何将 autodie 与非内置函数一起使用? [英] How to use autodie with non-builtins?

查看:36
本文介绍了如何将 autodie 与非内置函数一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

特别是我想将它用于 Imager 模块.很多函数和方法可能会失败,如果这并不意味着我的代码会到处都是 或 die Imager|$image->errstr; 短语,我更愿意.

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.

当然,如果除了使用 autodie 之外还有其他方法可以实现,我也会对此感兴趣.

Of course, if there's another way than using autodie to achieve that, I would be interested in that too.

推荐答案

autodie 仅适用于函数,而不是方法.这是因为它是词法范围的,而方法查找不能是词法范围的.autodie::hints 解释了如何告诉 autodie 关于用户定义的函数,但是不会对方法做任何事情.

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.

我不知道有什么方法可以让方法获得类似 autodie 的行为,除非模块内置了(例如 DBIRaiseError).

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).

您可以有一个子例程来进行检查,但它不会保存那么多代码,因为您仍然必须将正确的对象或类传递给它来调用 errstr.

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 与非内置函数一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆