如何处理 Perl 中方法链中的错误?
处理 Perl 中方法链接中抛出的异常的最佳方法是什么? 如果任何链接的方法抛出异常,我想分配 0 或 undef 值
代码示例:
my $x = $obj->get_obj->get_other_obj->get_another_obj->do_something;
最好的方法是什么? 我每次都必须用 try/catch/finally 语句换行吗? 我想要应用这个的上下文是:我使用 Catalyst 和 DBIC 进行 Web 开发,并且我做了很多链接的结果集,如果其中一些结果集抛出异常,我只想分配一个值 0 或 undef,然后处理它模板中的错误(我正在使用模板工具包)。如果有另一种方法可以做到这一点,而不用将每个调用包装在 try/catch 中,请告诉我。如果您知道在相同上下文(Catalyst/DBIC/TT)中处理此类错误的更好方法,请提出建议。 一个实际的例子是,当用户搜索某物但该物不存在时。
What is the best way to deal with exceptions threw in a method chaining in Perl?
I want to assign a value of 0 or undef if any of the methods chained throw an exception
Code sample:
my $x = $obj->get_obj->get_other_obj->get_another_obj->do_something;
What the best way to do it?
Do I have to wrap in a try/catch/finally statement everytime?
The context I want to apply this is: Im working in web development using Catalyst and DBIC and I do a lot of chained resultsets and if some of this resultset throw an exception I just want to assign a value of 0 or undef and then treat this error in the template (Im using Template Toolkit). If there is another way to do that without wrapping every call in try/catch, please let me know. If you know a better way to treat this type of error in the same context (Catalyst/DBIC/TT), please suggest.
A practical example would be when the user search for something and this does not exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过在失败时返回一个空对象来处理这个问题。该对象通过简单地返回自身来响应每个方法,因此它会继续这样做,直到耗尽剩余的方法。最后,您查看
$x
看看它是您期望的结果还是这个 null 对象。下面是这样的一个例子:
对于每个调用的方法,AUTOLOAD 都会拦截它并返回空对象。
当您遇到错误时,您将返回这些 Null 对象之一。在方法链的中间,您仍然会返回一个对象,这样当您调用下一个方法时,Perl 就不会崩溃。
在链的末尾,您可以检查返回的内容以查看它是否为 Null 对象。如果这就是你得到的,那么就发生了不好的事情。
这就是基本的想法。您可以改进 Null 类,使其记住消息及其创建位置,或者添加一些多态方法(例如
sub is_success { 0 }
),使其能够与你期望得到的物品。我以为我在某处写过很长的文章,但现在我找不到了。
更新:找到了其中一些著作:
I handle this by returning a null object at the point of failure. That object responds to every method by simply returning itself, so it keeps doing that until it eats up the remaining methods. At the end, you look in
$x
to see if it's the result you expected or this null object.Here's an example of such a thing:
For every method called, the AUTOLOAD intercepts it and returns the empty object.
When you run into an error, you return one of these Null objects. In the middle of a method chain you still get an object back so Perl doesn't blow up when you call the next method.
At the end of the chain, you can check what you got back to see if it's a Null object. If that's what you got, something bad happened.
That's the basic idea. You can improve on the Null class to make it remember a message and where it was created, or add some polymorphic methods (such as
sub is_success { 0 }
) to make it play nicely with the interfaces of the objects you expected to get.I thought I had written something long about this somewhere, but now I can't find it.
UPDATE: found some of those writings:
您可以编写一个标量方法,该方法将在错误处理中包装方法链:
使用它:
$try
方法的第一行还允许使用以下语法:You can write a scalar method that will wrap a method chain in error handling:
to use it:
The first line of the
$try
method also allows the following syntax:一种想法是创建一个类,使用
overload
在实例对象在字符串/数字/布尔上下文中进行评估,但仍然允许对其调用方法。AUTOLOAD
方法始终可以返回$self
,从而允许方法链传播相同的错误。One idea would be to create a class that uses
overload
to return a false value when an instance object is evaluated in string/number/boolean contexts, but would still allow methods to be called on it. AnAUTOLOAD
method could always return$self
allowing a method chain to propagate the same error.