📄 ch31_18.htm
字号:
<html><head><title>use sigtrap (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="use sigtrap"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch31_17.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch31_01.htm">Chapter 31: Pragmatic Modules</a></td><td align="right" valign="top" width="172"><a href="ch31_19.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">31.18. use sigtrap</h2><blockquote><pre class="programlisting">use sigtrap;use sigtrap qw(stack-trace old-interface-signals); # same thinguse sigtrap qw(BUS SEGV PIPE ABRT);use sigtrap qw(die INT QUIT);use sigtrap qw(die normal-signals);use sigtrap qw(die untrapped normal-signals);use sigtrap qw(die untrapped normal-signals stack-trace any error-signals);use sigtrap 'handler' => \&my_handler, 'normal-signals';use sigtrap qw(handler my_handler normal-signals stack-trace error-signals);</pre></blockquote><p>The <tt class="literal">sigtrap</tt> pragma installs some simple signal handlers on yourbehalf so that you don't have to worry about them. This is useful insituations where an untrapped signal would cause your program tomisbehave, like when you have <tt class="literal">END {}</tt> blocks, object destructors, orother at-exit processing that needs to be run no matter how yourprogram happens to terminate.</p><p>The <tt class="literal">sigtrap</tt> pragma provides two simple signalhandlers for your use. One provides a Perl stack trace, and the otherthrows an ordinary exception via <tt class="literal">die</tt>. Alternately,you can supply your own handler for the pragma to install. You mayspecify predefined sets of signals to trap; you can also supply yourown explicit list of signals. The pragma can optionally installhandlers for only those signals that have not otherwise been handled.</p><p>Arguments passed to <tt class="literal">use sigtrap</tt> are processed in order. When auser-supplied signal name or the name of one of <tt class="literal">sigtrap</tt>'spredefined signal lists is encountered, a handler is immediatelyinstalled. When an option is encountered, this affects only thosehandlers installed later in processing the argument list.</p><h3 class="sect2">31.18.1. Signal Handlers</h3><p>These options affect which handler will be used for signals installed later:</p><dl><dt><b><tt class="literal">stack-trace</tt></b></dt><dd><p>This pragma-supplied handler outputs a Perl stack trace to <tt class="literal">STDERR</tt>and then tries to dump core. This is the default signal handler.</p></dd><dt><b><tt class="literal">die</tt></b></dt><dd><p>This pragma-supplied handler calls <tt class="literal">die</tt> via <tt class="literal">Carp::croak</tt>with a message indicating the signal caught.</p></dd><dt><b><tt class="literal">handler</tt><em class="replaceable">YOURHANDLER</em></b></dt><dd><p><em class="replaceable">YOURHANDLER</em> will be used as the handler forsignals installed later. <em class="replaceable">YOURHANDLER</em> canbe any value valid for assignment into <tt class="literal">%SIG</tt>.Remember that the proper functioning of many C library calls(particularly standard I/O calls) cannot be guaranteed within a signalhandler. Worse, it's hard to guess which bits of C library code arecalled from which bits of Perl code. (On the other hand, many of thesignals that <tt class="literal">sigtrap</tt> traps are pretty vile--they'regonna take you down anyway, so there's not much harm in<em class="emphasis">trying</em> to do something, now is there?)</p></dd></dl><p></p><h3 class="sect2">31.18.2. Predefined Signal Lists</h3><p>The <tt class="literal">sigtrap</tt> pragma has a few built-in lists of signals to trap:</p><dl><dt><b><tt class="literal">normal-signals</tt></b></dt><dd><p>These are the signals a program might normally expect to encounter,and which, by default, cause it to terminate. They are the<tt class="literal">HUP</tt>, <tt class="literal">INT</tt>, <tt class="literal">PIPE</tt>, and <tt class="literal">TERM</tt> signals.</p></dd><dt><b><tt class="literal">error-signals</tt></b></dt><dd><p>These are the signals that usually indicate a serious problem withthe Perl interpreter or with your program. They are the <tt class="literal">ABRT</tt>,<tt class="literal">BUS</tt>, <tt class="literal">EMT</tt>, <tt class="literal">FPE</tt>, <tt class="literal">ILL</tt>, <tt class="literal">QUIT</tt>, <tt class="literal">SEGV</tt>, <tt class="literal">SYS</tt>,and <tt class="literal">TRAP</tt> signals.</p></dd><dt><b><tt class="literal">old-interface-signals</tt></b></dt><dd><p>These are the signals that were trapped by default under an olderversion of <tt class="literal">sigtrap</tt>'s interface. They are <tt class="literal">ABRT</tt>, <tt class="literal">BUS</tt>,<tt class="literal">EMT</tt>, <tt class="literal">FPE</tt>, <tt class="literal">ILL</tt>, <tt class="literal">PIPE</tt>, <tt class="literal">QUIT</tt>, <tt class="literal">SEGV</tt>,<tt class="literal">SYS</tt>, <tt class="literal">TERM</tt>, and <tt class="literal">TRAP</tt>. If no signals or signals listsare passed to <tt class="literal">use sigtrap</tt>, this list is used.</p></dd></dl><p></p><p>If your platform does not implement a particular signal namedin the predefined lists, that signal name will be silently ignored.(The signal itself can't be ignored, because it doesn't exist.)</p><h3 class="sect2">31.18.3. Other Arguments to sigtrap</h3><dl><dt><b><tt class="literal">untrapped</tt></b></dt><dd><p>This token suppresses the installation of handlers for subsequentlylisted signals if they're already been trapped or ignored.</p></dd><dt><b><tt class="literal">any</tt></b></dt><dd><p>This token installs handlers for all subsequently listed signals. This is the default behavior.</p></dd><dt><b><em class="emphasis">signal</em></b></dt><dd><p>Any argument that looks like a signal name (that is, one matching thepattern <tt class="literal">/^[A-Z][A-Z0-9]*$/</tt>) requests <tt class="literal">sigtrap</tt> to handlethat signal.</p></dd><dt><b><em class="emphasis">number</em></b></dt><dd><p>A numeric argument requires the version number of the <tt class="literal">sigtrap</tt>pragma to be at least <em class="emphasis">number</em>. This works is just like most regularmodules that have a <tt class="literal">$VERSION</tt> package variable:<blockquote><pre class="programlisting">% perl -Msigtrap -le 'print $sigtrap::VERSION'1.02</pre></blockquote></p></dd></dl><p></p><h3 class="sect2">31.18.4. Examples of sigtrap</h3><p>Provide a stack trace for the old interface signals:<blockquote><pre class="programlisting">use sigtrap;</pre></blockquote>Same thing, but more explicitly:<blockquote><pre class="programlisting">use sigtrap qw(stack-trace old-interface-signals);</pre></blockquote>Provide a stack trace only on the four listed signals:<blockquote><pre class="programlisting">use sigtrap qw(BUS SEGV PIPE ABRT);</pre></blockquote>Die on an <tt class="literal">INT</tt> or a <tt class="literal">QUIT</tt> signal:<blockquote><pre class="programlisting">use sigtrap qw(die INT QUIT);</pre></blockquote>Die on any of <tt class="literal">HUP</tt>, <tt class="literal">INT</tt>, <tt class="literal">PIPE</tt>, or <tt class="literal">TERM</tt>:<blockquote><pre class="programlisting">use sigtrap qw(die normal-signals);</pre></blockquote>Die on <tt class="literal">HUP</tt>, <tt class="literal">INT</tt>, <tt class="literal">PIPE</tt>, or <tt class="literal">TERM</tt>--except don't change the behavior forsignals that have already been trapped or ignored elsewhere in theprogram:<blockquote><pre class="programlisting">use sigtrap qw(die untrapped normal-signals);</pre></blockquote>Die on receipt of any currently untrapped <tt class="literal">normal-signals</tt>;additionally, provide a stack backtrace on receipt of any of the<tt class="literal">error-signals</tt>:<blockquote><pre class="programlisting">use sigtrap qw(die untrapped normal-signals stack-trace any error-signals);</pre></blockquote>Install the routine <tt class="literal">my_handler</tt> as the handler for the<tt class="literal">normal-signals</tt>:<blockquote><pre class="programlisting">use sigtrap 'handler' => \&my_handler, 'normal-signals';</pre></blockquote>Install <tt class="literal">my_handler</tt> as the handler for the <tt class="literal">normal-signals</tt>;provide a Perl stack backtrace on receipt of any of the <tt class="literal">error-signals</tt>:<blockquote><pre class="programlisting">use sigtrap qw(handler my_handler normal-signals stack-trace error-signals);</pre></blockquote></p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch31_17.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch31_19.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">31.17. use re</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">31.19. use strict</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -