📄 ch04_09.htm
字号:
<html><head><title>Pragmas (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Pragmas"><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_08.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="ch05_01.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.9. Pragmas</h2><a name="INDEX-1237"></a><a name="INDEX-1238"></a><a name="INDEX-1239"></a><p>Many programming languages allow you to give hints to the compiler.In Perl, these hints are conveyed to the compiler with the<tt class="literal">use</tt> declaration. Some pragmas are:<blockquote><pre class="programlisting">use warnings;use strict;use integer;use bytes;use constant pi => ( 4 * atan2(1,1) );</pre></blockquote>Perl pragmas are all described in <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a>, but right now we'll just talkspecifically about a couple that are most useful with the materialcovered in this chapter.</p><p><a name="INDEX-1240"></a><a name="INDEX-1241"></a>Although a few pragmas are global declarations that affect globalvariables or the current package, most are lexically scopeddeclarations whose effects are constrained to last only until the endof the enclosing block, file, or <tt class="literal">eval</tt> (whichevercomes first). A lexically scoped pragma can be countermanded in aninner scope with a <tt class="literal">no</tt> declaration, which works justlike <tt class="literal">use</tt> but in reverse.</p><h3 class="sect2">4.9.1. Controlling Warnings</h3><a name="INDEX-1242"></a><p>To show how this works, we'll manipulate the <tt class="literal">warnings</tt> pragma to tellPerl whether to issue warnings for questionable practices:<blockquote><pre class="programlisting">use warnings; # Enable warnings from here till end of file....{ no warnings; # Disable warnings through end of block. ...}# Warnings are automatically enabled again here.</pre></blockquote>Once warnings are enabled, Perl complains about variables used onlyonce, variable declarations that mask other declarations in thesame scope, improper conversions of strings into numbers, usingundefined values as legitimate strings or numbers, trying to writeto files you only opened read-only (or didn't open at all), andmany other conditions documented in <a href="ch33_01.htm">Chapter 33, "Diagnostic Messages"</a>.</p><p><a name="INDEX-1243"></a><a name="INDEX-1244"></a>The <tt class="literal">use warnings</tt> pragma is the preferred way tocontrol warnings. Old programs could only use the <span class="option">-w</span>command-line switch or modify the global <tt class="literal">$^W</tt>variable:<blockquote><pre class="programlisting">{ local $^W = 0; ...}</pre></blockquote>It's much better to use the <tt class="literal">use warnings</tt> and<tt class="literal">no warnings</tt> pragmas. A pragma is better because ithappens at compile time, because it's a lexical declaration andtherefore cannot affect code it wasn't intended to affect, and because(although we haven't shown you in these simple examples) it affordsfine-grained control over discrete classes of warnings. For moreabout the <tt class="literal">warnings</tt> pragma, including how to convertmerely noisy warnings into fatal errors, and how to override thepragma to turn on warnings globally even if a module says not to, see<tt class="literal">use warnings</tt> in <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a>.</p><h3 class="sect2">4.9.2. Controlling the Use of Globals</h3><a name="INDEX-1245"></a><p><a name="INDEX-1246"></a>Another commonly seen declaration is the <tt class="literal">use strict</tt>pragma, which has several functions, one of which is to control theuse of global variables. Normally, Perl lets you create new globals(or all too often, step on old globals) just by mentioning them. Novariable declarations are necessary--by default, that is. Becauseunbridled use ofglobals can make large programs or modules painful to maintain, youmay sometimes wish to discourage their accidental use. As an aid topreventing such accidents, you can say:<blockquote><pre class="programlisting">use strict 'vars';</pre></blockquote>This means that any variable mentioned from here to the end of the enclosingscope must refer either to a lexical variable or to an explicitly allowedglobal. If it's not one of those, a compilation error results.A global is explicitly allowed if one of the following is true:</p><ul><li><p>It's one of Perl's program-wide special variables (see <a href="ch28_01.htm">Chapter 28, "Special Names"</a>).</p></li><li><p>It's fully qualified with its package name (see <a href="ch10_01.htm">Chapter 10, "Packages"</a>),</p></li><li><p>It's imported into the current package (see <a href="ch11_01.htm">Chapter 11, "Modules"</a>).</p></li><li><p>It's masquerading as a lexically scoped variable via an <tt class="literal">our</tt> declaration. (This is the main reason we added <tt class="literal">our</tt> declarations to Perl.)</p></li></ul><p><a name="INDEX-1247"></a><a name="INDEX-1248"></a>Of course, there's always the fifth alternative--if the pragma provesburdensome, simply countermand it within an inner block using:<blockquote><pre class="programlisting">no strict 'vars'</pre></blockquote>You can also turn on strict checking of symbolic dereferences andaccidental use of barewords with this pragma. Normally people just say:<blockquote><pre class="programlisting">use strict;</pre></blockquote>to enable all three strictures. See the <tt class="literal">use strict</tt>entry in <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a> for more information.</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_08.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="ch05_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">4.8. Scoped Declarations</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">5. Pattern Matching</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 © 2001</a> O'Reilly & 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 + -