⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch04_06.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>goto (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 &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="goto"><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="ch04_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch04_01.htm">Chapter 4: Statements and Declarations</a></td><td align="right" valign="top" width="172"><a href="ch04_07.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">4.6. goto</h2><a name="INDEX-1161"></a><a name="INDEX-1162"></a><p>Although not for the faint of heart (nor for the pure of heart),Perl does support a <tt class="literal">goto</tt> operator.  There are threeforms: <tt class="literal">goto</tt><em class="replaceable">LABEL</em>, <tt class="literal">goto</tt><em class="replaceable">EXPR</em>, and <tt class="literal">goto &amp;</tt><em class="replaceable">NAME</em>.</p><p>The <tt class="literal">goto</tt><em class="replaceable">LABEL</em> formfinds the statement labeled with <em class="replaceable">LABEL</em> andresumes execution there.  It cant be used to jump into any constructthat requires initialization, such as a subroutine or a<tt class="literal">foreach</tt> loop.  It also can't be used to jump into aconstruct that has been optimized away (see <a href="ch18_01.htm">Chapter 18, "Compiling"</a>).  It can be used to go almostanywhere else within the current block or any block in your dynamicscope (that is, a block you were called from).  You can even<tt class="literal">goto</tt> out of subroutines, but it's usuallybetter to use some other construct.  The author of Perl has never feltthe need to use this form of <tt class="literal">goto</tt> (in Perl, thatis--C is another matter).</p><p><a name="INDEX-1163"></a>The <tt class="literal">goto</tt><em class="replaceable">EXPR</em> form is just a generalization of <tt class="literal">goto</tt><em class="replaceable">LABEL</em>.It expects the expression to produce a label name, whose locationobviously has to be resolved dynamically by the interpreter.  Thisallows for computed gotos per FORTRAN, but isn't necessarilyrecommended if you're optimizing for maintainability:<blockquote><pre class="programlisting">goto(("FOO", "BAR", "GLARCH")[$i]);         # hope 0 &lt;= i &lt; 3@loop_label = qw/FOO BAR GLARCH/;goto $loop_label[rand @loop_label];         # random teleport</pre></blockquote>In almost all cases like this, it's usually a far, farbetter idea to use the structured control flow mechanisms of<tt class="literal">next</tt>, <tt class="literal">last</tt>, or<tt class="literal">redo</tt> instead of resorting to a<tt class="literal">goto</tt>.  For certain applications, a hash ofreferences to functions or the catch-and-throw pair of<tt class="literal">eval</tt> and <tt class="literal">die</tt> for exceptionprocessing can also be prudent approaches.</p><p><a name="INDEX-1164"></a><a name="INDEX-1165"></a><a name="INDEX-1166"></a>The <tt class="literal">goto &amp;</tt><em class="replaceable">NAME</em> formis highly magical and sufficiently removed from the ordinary<tt class="literal">goto</tt> to exempt its users from the opprobrium towhich <tt class="literal">goto</tt> users are customarily subjected.  Itsubstitutes a call to the named subroutine for the currently runningsubroutine.  This behavior is used by <tt class="literal">AUTOLOAD</tt>subroutines to load another subroutine and then pretend that the othersubroutine was called in the first place.  After the<tt class="literal">goto</tt>, not even <tt class="literal">caller</tt> will beable to tell that this routine was called first.  The<tt class="literal">autouse</tt>, <tt class="literal">AutoLoader</tt>, and<tt class="literal">SelfLoader</tt> modules all use this strategy to definefunctions the first time they're called, and then to jump right tothem without anyone ever knowing the functions weren't there allalong.</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="ch04_05.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="ch04_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">4.5. Bare Blocks</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">4.7. Global Declarations</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 &copy; 2001</a> O'Reilly &amp; 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 + -