📄 ch04_10.htm
字号:
<html><head><title>The use strict Pragma (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#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="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_09.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch04_11.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">4.10. The use strict Pragma</h2><p>Perl tends to be a pretty permissive language. But maybe you wantPerl to impose a little discipline; that can be arranged with the<tt class="literal">use strict</tt><a name="INDEX-357" /><a name="INDEX-358" /> pragma.</p><p>A<em class="firstterm">pragma</em><a name="INDEX-359" />is a hint to a compiler, telling it something about the code. In thiscase, the <tt class="literal">use strict</tt> pragma tells Perl'sinternal compiler that it should enforce some good programming rulesfor the rest of this block or source file.</p><p>Why would this be important? Well, imagine that you'recomposing your program, and you type a line like this one:</p><blockquote><pre class="code">$bamm_bamm = 3; # Perl creates that variable automatically</pre></blockquote><p>Now, you keep typing for a while. After that line has scrolled offthe top of the screen, you type this line to increment the variable:</p><blockquote><pre class="code">$bammbamm += 1; # Oops!</pre></blockquote><p>Since Perl sees a new variable name (the <a name="INDEX-360" /> <a name="INDEX-361" />underscore <em class="emphasis">is</em>significant in a variable name), it creates a new variable andincrements that one. If you're lucky and smart, you'veturned on warnings, and Perl can tell you that you used one or bothof those global variable names only once in yourprogram<em class="emphasis">.</em> But if you're merely smart, youused each name more than once, and Perl won't be able to warnyou.</p><p>To tell Perl that you're ready to be more restrictive, put the<tt class="literal">use strict</tt> pragma at the top of your program (orin any block or file where you want to enforce these rules):</p><blockquote><pre class="code">use strict; # Enforce some good programming rules</pre></blockquote><p>Now, among other restrictions,<a href="#FOOTNOTE-115">[115]</a> Perl will insistthat you declare every new variable with<tt class="literal">my</tt>:<a href="#FOOTNOTE-116">[116]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-115" /><p>[115]To learn about theother restrictions, see the documentation for<tt class="literal">strict</tt>. The documentation for any pragma is filedunder that pragma's name, so the command <em class="emphasis">perldocstrict</em>(or your system's nativedocumentation method) should find it for you. In brief, the otherrestrictions require that strings be quoted in most cases, and thatreferences be true (hard) references. Neither of these restrictionsshould affect beginners in Perl.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-116" /><p>[116]There are some other ways todeclare variables, too.</p> </blockquote><blockquote><pre class="code">my $bamm_bamm = 3; # New lexical variable</pre></blockquote><p>Now if you try to spell it the other way, Perl can complain that youhaven't declared any variable called<tt class="literal">$bammbamm</tt>, so your mistake is automatically caughtat compile time.</p><blockquote><pre class="code">$bammbamm += 1; # No such variable: Compile time error</pre></blockquote><p>Of course, this applies only to new variables; Perl's builtinvariables, such as <tt class="literal">$_</tt> and <tt class="literal">@_</tt>never need to be declared.<a href="#FOOTNOTE-117">[117]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-117" /><p>[117]And, at least in somecircumstances, <tt class="literal">$a</tt> and <tt class="literal">$b</tt>won't need to be declared, because they're usedinternally by <tt class="literal">sort</tt>. So if you're testingthis feature, use other variable names than those two. The fact that<tt class="literal">use strict</tt> doesn't forbid these two is oneof the most frequently reported non-bugs in Perl.</p> </blockquote><p>If you add <tt class="literal">use strict</tt> to an already-writtenprogram, you'll generally get a flood of warning messages, soit's better to use it from the start, when it's needed.</p><p>Most people recommend that programs that are longer than a screenfulof text generally need <tt class="literal">use strict</tt>. And we agree.</p><p>From here on, most (but not all) of our examples will be written asif <tt class="literal">use strict</tt> is in effect, even where wedon't show it. That is, we'll generally declare variableswith <tt class="literal">my</tt> where it's appropriate. But, eventhough we don't always do so here, we encourage you to include<tt class="literal">use strict</tt> in your programs as often as possible.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_09.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch04_11.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">4.9. Notes on Lexical (my) Variables</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">4.11. The return Operator</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><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 + -