appb_06.htm

来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 166 行

HTM
166
字号
<html><head><title>Pragmas (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 &amp; 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="appb_05.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="appb_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">B.6. Pragmas</h2><p><a name="INDEX-1189" />Pragmasare special modules that come with each release of Perl and tellPerl's internal compiler something about your code.You've already used the <tt class="literal">strict</tt> pragma. Thepragmas available for your release of Perl should be listed in the<tt class="literal">perlmodlib</tt><a name="INDEX-1190" />manpage.</p><p>You use pragmas much like you'd use ordinary modules, with a<tt class="literal">use</tt><a name="INDEX-1191" />directive. Some pragmas are lexically scoped, like lexical("<tt class="literal">my</tt>") variables are, and theytherefore apply to the smallest enclosing block or file. Others mayapply to the entire program or to the current package. (If youdon't use any packages, the pragmas apply to your entireprogram.) Pragmas should generally appear near the top of your sourcecode. The documentation for each pragma should tell you howit's scoped.</p><a name="lperl3-APP-B-SECT-6.1" /><div class="sect2"><h3 class="sect2">B.6.1. The constant Pragma</h3><p>If you've used other languages, you've probably seen theability to declare constants in one way or another. Constants arehandy for making a setting just once, near the beginning of aprogram, but that can be easily updated if the need arises. Perl cando this with the package-scoped<tt class="literal">constant</tt><a name="INDEX-1192" /> pragma, which tells the compiler that agiven identifier has a constant value, which may thus be optimizedwherever it appears. For example:</p><blockquote><pre class="code">use constant DEBUGGING =&gt; 0;use constant ONE_YEAR =&gt; 365.2425 * 24 * 60 * 60;if (DEBUGGING) {  # This code will be optimized away unless DEBUGGING is turned on  ...}</pre></blockquote></div><a name="lperl3-APP-B-SECT-6.2" /><div class="sect2"><h3 class="sect2">B.6.2. The diagnostics Pragma</h3><p>Perl's diagnostic messages often seem somewhat cryptic, atleast the first time you see them. But you can always look them up inthe <tt class="literal">perldiag</tt> manpage to find out what they mean,and often a little about what's likely to be the problem andhow to fix it. But you can save yourself the trouble of searchingthat manpage if you use the<tt class="literal">diagnostics</tt><a name="INDEX-1193" /> pragma, which tells Perl to track downand print out the related information for any message. Unlike mostpragmas, though, this one is <em class="emphasis">not</em> intended foreveryday use, as it makes your program read the entire<tt class="literal">perldiag</tt> manpage just to get started. (This ispotentially a significant amount of overhead, both in terms of timeand memory.) Use this pragma only when you're debugging<em class="emphasis">and</em> expecting to get error message youdon't yet understand. It affects your entire program. Thesyntax is:</p><blockquote><pre class="code">use diagnostics;</pre></blockquote></div><a name="lperl3-APP-B-SECT-6.3" /><div class="sect2"><h3 class="sect2">B.6.3. The lib Pragma</h3><p>It's nearly always best to install modules in the standarddirectories, so that they're available for everyone, but onlythe system administrator can do that. If you install your ownmodules, you'll have to store them in your owndirectories -- so, how will Perl know where to find them?That's what the <tt class="literal">lib</tt><a name="INDEX-1194" /> pragma is all about. It tells Perl thatthe given directory is the first place to look for<a name="INDEX-1195" />modules. (That means that it's alsouseful for trying out a new release of a given module.) It affectsall modules loaded from this point on. The syntax is:</p><blockquote><pre class="code">use lib '/home/rootbeer/experimental';</pre></blockquote><p>Be sure to use a nonrelative pathname as the argument, sincethere's no telling what will be the current working directorywhen your program is run. This is especially important for CGIprograms (that is, programs run by a web server).</p></div><a name="lperl3-APP-B-SECT-6.4" /><div class="sect2"><h3 class="sect2">B.6.4. The strict Pragma</h3><p>You've been using <tt class="literal">usestrict</tt><a name="INDEX-1196" /> <a name="INDEX-1197" /> for a while already without having tounderstand that it's a pragma. It's lexically scoped, andit enforces some good programming rules. See its documentation tolearn what restrictions are available in your release of Perl.</p></div><a name="lperl3-APP-B-SECT-6.5" /><div class="sect2"><h3 class="sect2">B.6.5. The vars Pragma</h3><p>In the rare case that you truly need a global variable while<tt class="literal">use strict</tt> is in effect, you may declare it withthe <tt class="literal">vars</tt><a name="INDEX-1198" /> pragma.<a href="#FOOTNOTE-407">[407]</a> This package-scoped pragma tellsPerl that you are intentionally using one or more global variables:</p><blockquote class="footnote"> <a name="FOOTNOTE-407" /><p>[407]If your programwill never be used with a version of Perl prior to 5.6, you shoulduse the <tt class="literal">our</tt> keyword instead of the <tt class="literal">vars</tt>pragma.</p> </blockquote><blockquote><pre class="code">use strict;use vars qw/ $fred $barney /;$fred = "This is a global variable, but that's all right.\n";</pre></blockquote></div><a name="lperl3-APP-B-SECT-6.6" /><div class="sect2"><h3 class="sect2">B.6.6. The warnings Pragma</h3><p>Starting in Perl version 5.6, you may choose to have lexically scopedwarnings with the<tt class="literal">warnings</tt><a name="INDEX-1199" /> pragma.<a href="#FOOTNOTE-408">[408]</a> That is,rather than using the <tt class="literal">-w</tt><a name="INDEX-1200" /> option crudely to turn warnings on oroff for the entire program at once, you may specify that you want nowarnings about undefined values in just one section of code, whileother warnings should be available. This also serves as a signal tothe maintenance programmer that says, "I know that this codewould produce warnings, but I know what I'm doinganyway." See the documentation for this pragma to learn aboutthe categories of warnings available in your release ofPerl.<a name="INDEX-1201" /></p><blockquote class="footnote"> <a name="FOOTNOTE-408" /><p>[408]If your programmay be used with a version of Perl prior to 5.6, you should not usethe <tt class="literal">warnings</tt> pragma. </p> </blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appb_05.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="appb_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">B.5. Some Important Modules</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">B.7. Databases</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 &copy; 2002</a> O'Reilly &amp; 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 + =
减小字号Ctrl + -
显示快捷键?