📄 ch17_04.htm
字号:
<html><head><title>Unquoted Hash Keys (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="ch17_03.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="ch17_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">17.4. Unquoted Hash Keys</h2><p>Perl offers many shortcuts that can help the programmer. Here'sa handy one: you may omit the quote marks on some<a name="INDEX-1105" /> <a name="INDEX-1106" /> <a name="INDEX-1107" />hash keys.</p><p>Of course, you can't omit the quote marks on just<em class="emphasis">any</em> key, since a hash key may be any arbitrarystring. But keys are often simple. If the hash key is made up ofnothing but letters, digits, and underscores without starting with adigit, you <em class="emphasis">may</em> be able to omit the quote marks.This kind of simple string without quote marks is called a<em class="firstterm">bareword</em><a name="INDEX-1108" />,since it stands alone without quotes.</p><p>One place you are permitted to use this shortcut is the most commonplace a hash key appears: in the curly braces of a hash elementreference. For example, instead of <tt class="literal">$score{"fred"}</tt>,you could write simply <tt class="literal">$score{fred}</tt>. Since manyhash keys are simple like this, not using quotes is a realconvenience. But beware; if there's anything inside the curlybraces besides a bareword, Perl will interpret it as an expression.</p><p>Another place where hash keys appear is when assigning an entire hashusing a list of key-value pairs. The <a name="INDEX-1109" /> <a name="INDEX-1110" /><a name="INDEX-1111" />big arrow(<tt class="literal">=></tt>) is especially useful between a key and avalue, because (again, only if the key is a bareword) the big arrowquotes it for you:</p><blockquote><pre class="code"># Hash containing bowling scoresmy %score = ( barney => 195, fred => 205, dino => 30,);</pre></blockquote><p>This is the one important difference between the big arrow and acomma; a bareword to the left of the big arrow is implicitly quoted.(Whatever is on the right is left alone, though.) This feature of thebig arrow doesn't have to be used only for hashes, althoughthat's the most frequent use.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch17_03.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="ch17_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">17.3. Transforming Items from a List with map</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">17.5. More Powerful Regular Expressions</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 + -