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

📄 ch04_02.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>Data Types and Variables (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><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="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><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_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch04_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">4.2. Data Types and Variables</h2><p><a name="INDEX-219" />Perl has three basic data types:<em class="emphasis">scalars</em>, <em class="emphasis">arrays</em>, and<em class="emphasis">hashes</em>.</p><p>Scalars are essentially simple variables. They are preceded by adollar sign (<tt class="literal">$</tt>). A scalar is either a number, astring, or a reference. (A reference is a scalar that points toanother piece of data. References are discussed later in thischapter.) If you provide a string in which a number is expected orvice versa, Perl automatically converts the operand using fairlyintuitive rules.</p><p><a name="INDEX-220" />Arrays are orderedlists of scalars accessed with a numeric subscript (subscripts startat 0). They are preceded by an "at"sign (<tt class="literal">@</tt>).</p><p><a name="INDEX-221" />Hashes are unorderedsets of key/value pairs accessed with the keys as subscripts. Theyare preceded by a percent sign (<tt class="literal">%</tt>).</p><a name="perlnut2-CHP-4-SECT-2.1" /><div class="sect2"><h3 class="sect2">4.2.1. Numbers</h3><p><a name="INDEX-222" />Perl stores numbersinternally as either signed integers or double-precision,floating-point values. Numeric literals are specified by any of thefollowing floating-point or integer formats:</p><dl><dt><b><tt class="literal">12345</tt> </b></dt><dd>Integer</p></dd><dt><b><tt class="literal">-54321</tt></b></dt><dd>Negative integer</p></dd><dt><b><tt class="literal">12345.67</tt></b></dt><dd>Floating point</p></dd><dt><b><tt class="literal">6.02E23</tt></b></dt><dd>Scientific notation</p></dd><dt><b><tt class="literal">0xffff</tt></b></dt><dd>Hexadecimal</p></dd><dt><b><tt class="literal">0377</tt></b></dt><dd>Octal</p></dd><dt><b><tt class="literal">4_294_967_296</tt></b></dt><dd>Underline for legibility</p></dd></dl><p>Since Perl uses the comma as a list separator, you cannot use a commafor improving the <a name="INDEX-223" /><a name="INDEX-224" /><a name="INDEX-225" /><a name="INDEX-226" />legibility of a large number. Toimprove legibility, Perl allows you to use an underscore characterinstead. The underscore works only within literal numbers specifiedin your program, not in strings functioning as numbers or in dataread from somewhere else. Similarly, the leading<tt class="literal">0x</tt> for hex and <tt class="literal">0</tt> for octal workonly for literals. The automatic conversion of a string to a numberdoes not recognize these prefixes&#x2014;you must do an explicitconversion.</p><p>Be aware that in Perl 5.8, there are many changes in how Perl dealswith integers and floating-point numbers. Regardless of how yoursystem handles numbers and conversion between characters and numbers,Perl 5.8 works around system deficiencies to force more accuratenumber handling. Furthermore, whereas prior to 5.8 Perl usedfloating-point numbers exclusively in math operations, Perl 5.8 nowuses and stores integers in numeric conversions and in arithmeticoperations.</p></div><a name="perlnut2-CHP-4-SECT-2.2" /><div class="sect2"><h3 class="sect2">4.2.2. String Interpolation</h3><p><a name="INDEX-227" />Strings aresequences of characters. String literals are usually delimited byeither single (<tt class="literal">'</tt>) or double (<tt class="literal">"</tt>)quotes. Double-quoted string literals are subject to backslash andvariable interpolation, and single-quoted strings are not (except for<tt class="literal">\'</tt> and <tt class="literal">\\</tt>, used to put singlequotes and backslashes into single-quoted strings). You can embednewlines directly in your strings.</p><p><a href="ch04_02.htm#perlnut2-CHP-4-TABLE-1">Table 4-1</a> lists all the backslashed or escapecharacters that can be used in double-quoted strings.</p><a name="perlnut2-CHP-4-TABLE-1" /><h4 class="objtitle">Table 4-1. Double-quoted string representations </h4><table border="1" cellpadding="3"><tr><th><p>Code</p></th><th><p>Meaning</p></th></tr><tr><td><p><tt class="literal">\n</tt></p></td><td><p>Newline</p></td></tr><tr><td><p><tt class="literal">\r</tt></p></td><td><p>Carriage return</p></td></tr><tr><td><p><tt class="literal">\t</tt></p></td><td><p>Horizontal tab</p></td></tr><tr><td><p><tt class="literal">\f</tt></p></td><td><p>Form feed</p></td></tr><tr><td><p><tt class="literal">\b</tt></p></td><td><p>Backspace</p></td></tr><tr><td><p><tt class="literal">\a</tt></p></td><td><p>Alert (bell)</p></td></tr><tr><td><p><tt class="literal">\e</tt></p></td><td><p>ESC character</p></td></tr><tr><td><p><tt class="literal">\033</tt></p></td><td><p>ESC in octal</p></td></tr><tr><td><p><tt class="literal">\x7f</tt></p></td><td><p>DEL in hexadecimal</p></td></tr><tr><td><p><tt class="literal">\cC</tt></p></td><td><p>Ctrl-C</p></td></tr><tr><td><p><tt class="literal">\\</tt></p></td><td><p>Backslash</p></td></tr><tr><td><p><tt class="literal">\"</tt></p></td><td><p>Double quote</p></td></tr><tr><td><p><tt class="literal">\u</tt></p></td><td><p>Force next character to uppercase</p></td></tr><tr><td><p><tt class="literal">\l</tt></p></td><td><p>Force next character to lowercase</p></td></tr><tr><td><p><tt class="literal">\U</tt></p></td><td><p>Force all following characters to uppercase</p></td></tr><tr><td><p><tt class="literal">\L</tt></p></td><td><p>Force all following characters to lowercase</p></td></tr><tr><td><p><tt class="literal">\Q</tt></p></td><td><p>Backslash all following non-alphanumeric characters</p></td></tr><tr><td><p><tt class="literal">\E</tt></p></td><td><p>End <tt class="literal">\U</tt>, <tt class="literal">\L</tt>, or<tt class="literal">\Q</tt></p></td></tr></table><p><p><a name="INDEX-228" /><a name="INDEX-229" /><a name="INDEX-230" /><a href="ch04_02.htm#perlnut2-CHP-4-TABLE-2">Table 4-2</a>lists alternative quoting schemes that can be used in Perl. These areuseful in diminishing the number of commas and quotes you may have totype, and they allow you not to worry about escaping characters suchas backslashes when there are many instances in your data. Thegeneric forms allow you to use any non-alphanumeric, non-whitespacecharacters as delimiters in place of the slash(<tt class="literal">/</tt>). If the delimiters are single quotes, novariable interpolation is done on the pattern. Parentheses, brackets,braces, and angle brackets can be used as delimiters in theirstandard opening and closing pairs.</p><a name="perlnut2-CHP-4-TABLE-2" /><h4 class="objtitle">Table 4-2. Quoting syntax in Perl </h4><table border="1" cellpadding="3"><tr><th><p>Customary</p></th><th><p>Generic</p></th><th><p>Meaning</p></th><th><p>Interpolation</p></th></tr><tr><td><p><tt class="literal">''</tt></p></td><td><p><tt class="literal">q//</tt></p></td><td><p>Literal</p></td><td><p>No</p></td></tr><tr><td><p><tt class="literal">""</tt></p></td><td><p><tt class="literal">qq//</tt></p></td><td><p>Literal</p></td><td><p>Yes</p></td></tr><tr><td><p><tt class="literal">''</tt></p></td><td><p><tt class="literal">qx//</tt></p></td><td><p>Command</p></td><td><p>Yes</p></td></tr><tr><td><p><tt class="literal">( )</tt></p></td><td><p><tt class="literal">qw//</tt></p></td><td><p>Word list</p></td><td><p>No</p></td></tr><tr><td><p><tt class="literal">( )</tt></p></td><td>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -