Perl:关于%SIG
时间:2006-04-06 来源:xiaoquqi
【参考1】Perl uses a simple signal handling model: the %SIG hash contains names or references of user-installed signal handlers. These handlers will be called with an argument which is the name of the signal that triggered it. A signal may be generated intentionally from a particular keyboard sequence like control-C or control-Z, sent to you from an another process, or triggered automatically by the kernel when special events transpire, like a child process exiting, your process running out of stack space, or hitting file size limit.
【参考2】Perl 提供了%SIG 这个特殊的HASH,通过定义信号响应函数,可以捕捉die及一些warning的信息,并将这些信息打印到web页上。但为了尽可能早的加载这些代码,最好将信号捕捉代码放到BEGIN块中,这样就能保证程序一执行就先执行异常捕捉这段代码了。
BEGIN {
# fatal handler setting.
$SIG{__DIE__} = $SIG{__WARN__} = \&some_func;
}
以下是一个简单的例程,定义了一个叫handler_fatal处理函数来处理意外错误信息。
# fatal handler setting.
$SIG{__DIE__} = $SIG{__WARN__} = \&some_func;
}
以下是一个简单的例程,定义了一个叫handler_fatal处理函数来处理意外错误信息。
#!/usr/bin/perl -w use strict BEGIN { # fatal handler setting. $SIG{__DIE__} = $SIG{__WARN__} = \&handler_fatal; } # some perl code goes here ...... sub handler_fatal { print "Content-type: text/html\n\n"; print "@_"; }
相关阅读 更多 +
排行榜 更多 +