📄 ch02_03.htm
字号:
<html><head><title>Built-in Data Types (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="Built-in Data Types"><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="ch02_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch02_01.htm">Chapter 2: Bits and Pieces</a></td><td align="right" valign="top" width="172"><a href="ch02_04.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">2.3. Built-in Data Types</h2><a name="INDEX-426"></a><a name="INDEX-427"></a><a name="INDEX-428"></a><p>Before we start talking about various kinds of tokens you can buildfrom characters, we need a few more abstractions. To be specific, we needthree data types.</p><p>Computer languages vary in how many and what kinds of data types theyprovide. Unlike some commonly used languages that provide manyconfusing types for similar kinds of values, Perl provides just a fewbuilt-in data types. Consider C, in which you might run into <tt class="literal">char</tt>,<tt class="literal">short</tt>, <tt class="literal">int</tt>, <tt class="literal">long</tt>, <tt class="literal">long long</tt>, <tt class="literal">bool</tt>, <tt class="literal">wchar_t</tt>,<tt class="literal">size_t</tt>, <tt class="literal">off_t</tt>, <tt class="literal">regex_t</tt>, <tt class="literal">uid_t</tt>, <tt class="literal">u_longlong_t</tt>,<tt class="literal">pthread_key_t</tt>, <tt class="literal">fp_exception_field_type</tt>, and so on. That's justsome of the integer types! Then there are floating-point numbers, andpointers, and strings.</p><p><a name="INDEX-429"></a>All these complicated types correspond to just one type in Perl: thescalar. (Usually Perl's simple data types are all you need, but ifnot, you're free to define fancy dynamic types using Perl'sobject-oriented features--see <a href="ch12_01.htm">Chapter 12, "Objects"</a>.)Perl's three basic data types are: <em class="emphasis">scalars</em>,<em class="emphasis">arrays</em> of scalars, and<em class="emphasis">hashes</em> of scalars (also known as<em class="emphasis">associative arrays</em>). Some people may prefer tocall these <em class="emphasis">data structures</em> rather than types.That's okay.</p><p><a name="INDEX-430"></a><a name="INDEX-431"></a><a name="INDEX-432"></a><a name="INDEX-433"></a><a name="INDEX-434"></a><a name="INDEX-435"></a><a name="INDEX-436"></a>Scalars are thefundamental type from which more complicated structures are built. Ascalar stores a single, simple value--typically a string or a number.Elements of this simple type may be combined into either of thetwo aggregate types. An <em class="emphasis">array</em> is an ordered listof scalars that you access with an integer subscript (or index). Allindexing in Perl starts at 0. Unlike many programming languages,however, Perl treats negative subscripts as valid: instead of countingfrom the beginning, negative subscripts count back from the end ofwhatever it is you're indexing into. (This applies to varioussubstring and sublist operations as well as to regular subscripting.)A <em class="emphasis">hash</em>, on the other hand, is an unordered set of<em class="emphasis">key/value</em> pairs that you access using strings(the <em class="emphasis">keys</em>) as subscripts to look up the scalars(the <em class="emphasis">values</em>) corresponding to a given key.Variables are always one of these three types. (Other than variables,Perl also has other abstractions that you can think of as data types,such as filehandles, directory handles, formats, subroutines, symboltables, and symbol table entries.)</p><p><a name="INDEX-437"></a><a name="INDEX-438"></a>Abstractions are wonderful, and we'll collect more of them as we goalong, but they're also useless in a way. You can't do anything withan abstraction directly. That's why computer languages have syntax.We need to introduce you to the various kinds of syntactic terms youcan use to pull your abstract data into expressions. We like to usethe technical term <em class="emphasis">term</em> when we want to talk in terms of thesesyntactic units. (Hmm, this could get terminally confusing. Justremember how your math teacher used to talk about the <em class="emphasis">terms</em> of anequation, and you won't go terribly wrong.)</p><p>Just like the terms in a math equation, the purpose of most terms inPerl is to produce values for operators like addition andmultiplication to operate on. Unlike in a math equation, however, Perlhas to <em class="emphasis">do</em> something with the values it calculates, not just thinkwith a pencil in its hand about whether the two sides of the equationare equal. One of the most common things to do with a value is tostore it somewhere:<blockquote><pre class="programlisting">$x = $y;</pre></blockquote></p><p><a name="INDEX-439"></a><a name="INDEX-440"></a><a name="INDEX-441"></a> That's an example ofthe <em class="emphasis">assignment</em> operator (not the numeric equalityoperator, which is spelled <tt class="literal">==</tt> in Perl). Theassignment gets the value from <tt class="literal">$y</tt> and puts it into<tt class="literal">$x</tt>. Notice that we aren't using the term<tt class="literal">$x</tt> for its value; we're using it for its location.(The old value of <tt class="literal">$x</tt> gets clobbered by theassignment.) We say that <tt class="literal">$x</tt> is an<em class="emphasis">lvalue</em>, meaning it's the sort of storage locationwe can use on the left side of an assignment. We say that<tt class="literal">$y</tt> is an <em class="emphasis">rvalue</em> because it'sused on the right side.</p><p><a name="INDEX-442"></a><a name="INDEX-443"></a>There's also a third kind of value, called a <em class="emphasis">temporary</em> value, thatyou need to understand if you want to know what Perl is really doingwith your lvalues and rvalues. If we do some actual math and say:<blockquote><pre class="programlisting">$x = $y + 1;</pre></blockquote>Perl takes the rvalue <tt class="literal">$y</tt> and adds the rvalue <tt class="literal">1</tt> to it, whichproduces a temporary value that is eventually assigned to the lvalue<tt class="literal">$x</tt>. It may help you to visualize what is going on if we tell youthat Perl stores these temporary values in an internal structure calleda <em class="emphasis">stack</em>.<a href="#FOOTNOTE-4">[4]</a> The termsof an expression (the ones we're talking about in this chapter) tend topush values onto the stack, while the operators of the expression(which we'll discuss in the next chapter) tend to pop them back off thestack, perhaps leaving another temporary result on the stack for thenext operator to work with. The pushes and pops all balance out--bythe time the expression is done, the stack is entirely empty (or asempty as it was when we started). More about temporary values later.<a name="INDEX-444"></a></p><blockquote class="footnote"><a name="FOOTNOTE-4"></a><p>[4] A stack works just like one of thosespring-loaded plate dispensers you see in a buffet restaurant--you can<em class="emphasis">push</em> plates onto the top of the stack, or you can <em class="emphasis">pop</em> them offagain (to use the Comp. Sci. vernacular).</p></blockquote><p>Some terms can only be rvalues, such as the <tt class="literal">1</tt>above, while others can serve as either lvalues or rvalues. Inparticular, as the assignments above illustrate, a variable mayfunction as either. And that's what our next section is about.</p><a name="INDEX-445"></a><a name="INDEX-446"></a><!-- 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="ch02_02.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="ch02_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">2.2. Molecules</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">2.4. Variables</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 + -