📄 ch16_17.htm
字号:
<HTML><HEAD><TITLE>Recipe 16.16. Temporarily Overriding a Signal Handler (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:44:10Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"><LINKREL="prev"HREF="ch16_16.htm"TITLE="16.15. Installing a Signal Handler"><LINKREL="next"HREF="ch16_18.htm"TITLE="16.17. Writing a Signal Handler"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_16.htm"TITLE="16.15. Installing a Signal Handler"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.15. Installing a Signal Handler"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch16_01.htm"TITLE="16. Process Management and Communication"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_18.htm"TITLE="16.17. Writing a Signal Handler"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.17. Writing a Signal Handler"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch16-22150">16.16. Temporarily Overriding a Signal Handler</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005555">Problem<ACLASS="indexterm"NAME="ch16-idx-1000006396-0"></A></A></H3><PCLASS="para">You want to install a signal handler only for a particular subroutine. For instance, your subroutine catches SIGINT, and you don't want to disturb SIGINT handling outside the subroutine.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005558">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">local</CODE><ACLASS="indexterm"NAME="ch16-idx-1000006403-0"></A> to temporarily override a signal's behavior:</P><PRECLASS="programlisting"># the signal handlersub ding { $SIG{INT} = \&ding; warn "\aEnter your name!\n";}# prompt for name, overriding SIGINTsub get_name { local $SIG{INT} = \&ding; my $name; print "Kindly Stranger, please enter your name: "; chomp( $name = <> ); return $name;}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005576">Discussion</A></H3><PCLASS="para">You must use <CODECLASS="literal">local</CODE> rather than <CODECLASS="literal">my</CODE> to save away one value out of <CODECLASS="literal">%SIG</CODE>. The change remains in effect throughout the execution of that block, including in anything called from it. In this case, that's the <CODECLASS="literal">get_name</CODE> subroutine. If the signal is delivered while another function that your function calls is running, your signal handler is triggered - unless the called subroutine installs its own signal handler. The previous value of the hash is automatically restored when the block exits. This is one of the (few) places where dynamic scoping is more convenient than confusing.<ACLASS="indexterm"NAME="ch16-idx-1000006402-0"></A><ACLASS="indexterm"NAME="ch16-idx-1000006402-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch16-pgfId-1000005579">See Also</A></H3><PCLASS="para"><ACLASS="xref"HREF="ch10_14.htm"TITLE="Saving Global Values">Recipe 10.13</A>; <ACLASS="xref"HREF="ch16_16.htm"TITLE="Installing a Signal Handler">Recipe 16.15</A>; <ACLASS="xref"HREF="ch16_19.htm"TITLE="Catching Ctrl-C">Recipe 16.18</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_16.htm"TITLE="16.15. Installing a Signal Handler"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.15. Installing a Signal Handler"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_18.htm"TITLE="16.17. Writing a Signal Handler"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 16.17. Writing a Signal Handler"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.15. Installing a Signal Handler</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">16.17. Writing a Signal Handler</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -