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

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

问题描述

我有一些关于人们如何在Perl中使用异常的问题。我已经添加了一些关于异常的背景说明,如果你愿意的话请跳过这个说明,但是请稍等一下阅读问题并回复他们。



谢谢。 >

Perl异常背景



Perl有一个非常基本的内置异常系统,为更复杂的



例如 die我吃了一个bug.\\\
;
用字符串引发异常分配给 $ @



你也可以抛出一个对象,而不是一个字符串: die BadBug-> new('我吃了一个bug'); / code>



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

  $ SIG {__ DIE__} = sub {
我的$ e = shift;
$ e = ExceptionObject-> new($ e)除非有福了$ e;
死$ e;
}

此模式用于多个CPAN模块。但是perlvar说:


由于执行错误,
$ SIG { DIE }勾在eval()中被称为even
。不要使用
将$ @或
中的挂起异常重写为替代
CORE :: GLOBAL :: die()的奇怪替代。远程的这个奇怪的
操作可能会固定在
的未来发行版中,所以$ SIG { DIE }
只有在您的程序为
时才会被调用即将退出,原来是
的意图。


所以现在我想知道在sigdie中是否存在异常异常。



问题




  1. 你是否使用异常对象?如果是这样,哪一个为什么?如果不是,为什么不呢?


  2. 如果你不使用异常对象,会诱使你使用它们?


  3. 如果你使用异常对象,你对它们有什么憎恨,那可能会更好?


  4. 在DIE处理程序中一个坏主意?


  5. 我应该在哪里反对我的例外?在我的eval {}包装器中?在一个sigdie处理程序中?


  6. 是否有任何有关例外情况的文章,文章或其他资源,在Perl中,您发现有用或有启发性。


交叉发布在 Perlmonks

解决方案

我不经常使用异常对象;主要是因为字符串通常足够而且涉及较少的工作。这是因为程序通常没有什么可以做的例外。如果可以避免这个例外,那么这个例外就不会造成这种情况。



如果你可以对异常做一些事情,那就用对象。如果你只是要杀死程序(或某个子集,比如一个网络请求),那么可以节省一些精心设计的对象层次结构,这些层次结构只能包含一个消息。



至于第4号;不得使用 $ SIG {__ DIE __} 。它不组成;如果一个模块希望sigdie以某种方式工作,另一个模块被加载,使其工作的其他方式,这些模块不能再在同一个程序中使用。所以不要这样做。



如果你想使用对象,只要做非常无聊的 die Object-> new(.. 。)。这可能不是令人兴奋的,因为某些超级棒的魔法,但它总是有效,代码完全符合它所说的。


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.

Thanks.

Background on Perl Exceptions

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

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

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

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;
}

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

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.

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

The Questions

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

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

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

  4. Is objectifying exceptions in the DIE handler a bad idea?

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

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

Cross-posted at 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.

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.

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.

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天全站免登陆