你在你的 Perl 程序中使用异常类吗?为什么或者为什么不? [英] Do you use an exception class in your Perl programs? Why or why not?

查看:15
本文介绍了你在你的 Perl 程序中使用异常类吗?为什么或者为什么不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多关于人们如何在 Perl 中使用异常的问题.我已经包含了一些关于例外情况的背景说明,如果您愿意,可以跳过此部分,但请花点时间阅读问题并做出回应.

I've got a bunch of questions about how people use exceptions in Perl. I've included some background notes on exceptions, skip this if you want, but please take a moment to read the questions and respond to them.

谢谢.

Perl 有一个非常基本的内置异常系统,它为更复杂的使用提供了一个跳板.

Perl has a very basic built-in exception system that provides a spring-board for more sophisticated usage.

例如 die "I ate a bug. "; 抛出一个异常,并将字符串分配给 $@.

For example die "I ate a bug. "; throws an exception with a string assigned to $@.

你也可以抛出一个对象,而不是一个字符串:die BadBug->new('I ate a bug.');

You can also throw an object, instead of a string: die BadBug->new('I ate a bug.');

您甚至可以安装信号处理程序来捕获 SIGDIE 伪信号.这是一个处理程序,如果异常还没有,它会将异常作为对象重新抛出.

You can even install a signal handler to catch the SIGDIE psuedo-signal. Here's a handler that rethrows exceptions as objects if they aren't already.

$SIG{__DIE__} = sub { 
    my $e = shift; 
    $e = ExceptionObject->new( $e ) unless blessed $e;
    die $e;
}

此模式用于许多 CPAN 模块.但 perlvar 说:

This pattern is used in a number of CPAN modules. but perlvar says:

由于实施故障,$SIG{DIE} 钩子被称为偶数在 eval() 中.不要用它来重写 $@ 中的待处理异常,或作为覆盖的奇怪替代品核心::全局::死().这个奇怪的远距离的动作可以固定在一个未来发布以便 $SIG{DIE}仅在您的程序是即将退出,就像原来的一样意图.任何其他用途均已弃用.

Due to an implementation glitch, the $SIG{DIE} hook is called even inside an eval(). Do not use this to rewrite a pending exception in $@ , or as a bizarre substitute for overriding CORE::GLOBAL::die() . This strange action at a distance may be fixed in a future release so that $SIG{DIE} is only called if your program is about to exit, as was the original intent. Any other use is deprecated.

所以现在我想知道在 sigdie 中对象化异常是否是邪恶的.

So now I wonder if objectifying exceptions in sigdie is evil.

  1. 你使用异常对象吗?如果是,是哪一个,为什么?如果没有,为什么不呢?

  1. Do you use exception objects? If so, which one and why? If not, why not?

如果你不使用异常对象,什么会吸引你使用它们?

If you don't use exception objects, what would entice you to use them?

如果您确实使用了异常对象,您讨厌它们的哪些方面,还有什么可以更好的?

If you do use exception objects, what do you hate about them, and what could be better?

在 DIE 处理程序中将异常对象化是一个坏主意吗?

Is objectifying exceptions in the DIE handler a bad idea?

我应该在哪里物化我的例外?在我的 eval{} 包装器中?在 sigdie 处理程序中?

Where should I objectify my exceptions? In my eval{} wrapper? In a sigdie handler?

是否有任何关于一般和 Perl 异常的论文、文章或其他资源对您有用或有启发.

Are there any papers, articles or other resources on exceptions in general and in Perl that you find useful or enlightening.

交叉发布于 Perlmonks.

推荐答案

我不经常使用异常对象;主要是因为一个字符串通常就足够了并且涉及的工作更少.这是因为程序通常对异常无能为力.如果它可以避免异常,它首先就不会导致它.

I don't use exception objects very often; mostly because a string is usually enough and involves less work. This is because there is usually nothing the program can do about the exception. If it could have avoided the exception, it wouldn't have caused it in the first place.

如果您可以对异常做些什么,请使用对象.如果您只是要终止程序(或某个子集,例如 Web 请求),则不必费力地想出一个复杂的对象层次结构,这些层次结构仅包含一条消息.

If you can do something about the exceptions, use objects. If you are just going to kill the program (or some subset, say, a web request), save yourself the effort of coming up with an elaborate hierarchy of objects that do nothing more than contain a message.

至于数字 4;$SIG{__DIE__} 不应该被使用.它不构成;如果一个模块希望 sigdie 以一种方式工作,而另一个模块被加载以使其以其他方式工作,那么这些模块就不能再在同一个程序中使用了.所以不要那样做.

As for number 4; $SIG{__DIE__} should never be used. It doesn't compose; if one module expects sigdie to work in one way, and another module is loaded that makes it work some other way, those modules can't be used in the same program anymore. So don't do that.

如果您想使用对象,只需执行非常无聊的die Object->new( ... ).它可能不会像某个地方的一些超级神奇的魔法那样令人兴奋,但它总是有效并且代码完全按照它所说的去做.

If you want to use objects, just do the very-boring die Object->new( ... ). It may not be exciting as some super-awesome magic somewhere, but it always works and the code does exactly what it says.

这篇关于你在你的 Perl 程序中使用异常类吗?为什么或者为什么不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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