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

📄 ch04_07.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Global Declarations (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="Global Declarations"><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_06.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_08.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.7. Global Declarations</h2><a name="INDEX-1167"></a><a name="INDEX-1168"></a><p><a name="INDEX-1169"></a>Subroutine and format declarations are global declarations.  No matterwhere you place them, what they declare is global (it's local to apackage, but packages are global to the program, so everything in a packageis visible from anywhere).A global declaration can be put anywhere a statement can,but it has no effect on the execution of the primary sequence ofstatements--the declarations take effect at compile time.</p><p><a name="INDEX-1170"></a><a name="INDEX-1171"></a><a name="INDEX-1172"></a>This means you can't conditionally declare subroutines or formats byhiding them from the compiler inside a run-time conditional like an<tt class="literal">if</tt>, since only the interpreter pays attention to those conditions.Subroutine and format declarations (and <tt class="literal">use</tt> and <tt class="literal">no</tt> declarations)are seen by the compiler no matter where they occur.</p><p>Global declarations are typically put at the beginning or the end ofyour program, or off in some other file.  However, if you're declaringany lexically scoped variables (see the next section), you'll want to make sureyour format or subroutine definition falls within the scope of thevariable declarations if you expect it to be able to access thoseprivate variables.</p><p><a name="INDEX-1173"></a><a name="INDEX-1174"></a><a name="INDEX-1175"></a>Note that we sneakily switched from talking about declarations todefinitions.  Sometimes it helps to split the <em class="emphasis">definition</em>of the subroutine from its <em class="emphasis">declaration</em>.  The onlysyntactic difference between the two is that the definition supplies a<em class="replaceable">BLOCK</em> containing the code to be executed,while the declaration doesn't.  (A subroutine definition acts as itsown declaration if no declaration has been seen.)  Splitting thedefinition from the declaration allows you to put the subroutinedeclaration at the front of the file and the definition at the end(with your lexically scoped variable declarations happily in the middle):<blockquote><pre class="programlisting">sub count (@);       # Compiler now knows how to call count().my $x;               # Compiler now knows about lexical variable.$x = count(3,2,1);   # Compiler can validate function call.sub count (@) { @_ } # Compiler now knows what count() means.</pre></blockquote>As this example shows, subroutines don't actually have to be definedbefore calls to them can be compiled (indeed, the definition can evenby delayed until first use, if you use autoloading), but declaringsubroutines helps the compiler in various ways and gives you moreoptions in how you can call them.</p><p><a name="INDEX-1176"></a><a name="INDEX-1177"></a><a name="INDEX-1178"></a>Declaring a subroutine allows it to be used without parentheses, as ifit were a built-in operator, from that point forward in thecompilation.  (We used parentheses to call <tt class="literal">count</tt> in the lastexample, but we didn't actually need to.)  You can declare a subroutinewithout defining it just by saying:<blockquote><pre class="programlisting">sub myname;$me = myname $0             or die "can't get myname";</pre></blockquote>A bare declaration like that declares the function to be a listoperator, not a unary operator, so be careful to use <tt class="literal">or</tt>there instead of <tt class="literal">||</tt>. The <tt class="literal">||</tt> operator binds too tightly to useafter list operators, though you can always use parentheses around thelist operators arguments to turn the list operator back into somethingthat behaves more like a function call.  Alternatively, you can use theprototype <tt class="literal">($)</tt> to turn thesubroutine into a unary operator:<blockquote><pre class="programlisting">sub myname ($);$me = myname $0             || die "can't get myname";</pre></blockquote>That now parses as you'd expect, but you still ought to get in thehabit of using <tt class="literal">or</tt> in that situation.  For more onprototypes, see<a href="ch06_01.htm">Chapter 6, "Subroutines"</a>.</p><p><a name="INDEX-1179"></a><a name="INDEX-1180"></a>You <em class="emphasis">do</em> need to define the subroutine at some point, or you'll get anerror at run time indicating that you've called an undefinedsubroutine.  Other than defining the subroutine yourself, there areseveral ways to pull in definitions from elsewhere.</p><p><a name="INDEX-1181"></a>You can load definitions from other files with a simple <tt class="literal">require</tt>statement; this was the best way to load files in Perl 4, but thereare two problems with it.  First, the other file will typicallyinsert subroutine names into a package (a symbol table) of its ownchoosing, not your packages.  Second, a <tt class="literal">require</tt> happens at run time,so it occurs too late to serve as a declaration in the file invokingthe <tt class="literal">require</tt>.  There are times, however, when delayed loading iswhat you want.</p><p><a name="INDEX-1182"></a><a name="INDEX-1183"></a><a name="INDEX-1184"></a><a name="INDEX-1185"></a>A more useful way to pull in declarations and definitions is with the<tt class="literal">use</tt> declaration, which effectively <tt class="literal">require</tt>s the module at compiletime (because <tt class="literal">use</tt> counts as a <tt class="literal">BEGIN</tt> block) and then lets youimport some of the module's declarations into your own program.Thus <tt class="literal">use</tt> can be considered a kind of global declaration, in that itimports names at compile time into your own (global) package just as ifyou'd declared them yourself.  See the section <a href="ch10_01.htm#ch10-sect-st">Section 4.1, "Symbol Tables"</a> in<a href="ch10_01.htm">Chapter 10, "Packages"</a>, for low-level mechanics on how importationworks between packages; <a href="ch11_01.htm">Chapter 11, "Modules"</a>, for how to set up amodule's imports and exports; and <a href="ch18_01.htm">Chapter 18, "Compiling"</a> for anexplanation of <tt class="literal">BEGIN</tt> and its cousins, <tt class="literal">CHECK</tt>, <tt class="literal">INIT</tt>, and <tt class="literal">END</tt>,which are also global declarations of a sort because they're dealt withat compile time and can have global effects.</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_06.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_08.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">4.6. goto</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.8. Scoped 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 + -