📄 00000019.htm
字号:
<BR> <P>Natural languages are good at both because people are continually trying <BR> to express both easy things and hard things, so the language evolves to <BR> handle both. Perl was designed first of all to evolve, and indeed it has <BR> evolved. Many people have contributed to the evolution of Perl over the <BR> years. We often joke that a camel is a horse designed by a committee, but <BR> if you think about it, the camel is pretty well adapted for life in the <BR> desert. The camel has evolved to be relatively self-sufficient.[4] </P> <BR> <BR> <BLOCKQUOTE class=footnote> <BR> <P>[4] On the other hand, the camel has not evolved to smell good. Neither <BR> has Perl. </P> <BR> </BLOCKQUOTE> <BR> <BR> <P>Now when someone utters the word &quot;linguistics&quot;, many people <BR> think of one of two things. Either they think of words, or they think of <BR> sentences. But words and sentences are just two handy ways to &quot;chunk&quot; <BR> speech. Either may be broken down into smaller units of meaning, or combined <BR> into larger units of meaning. And the meaning of any unit depends heavily <BR> on the syntactic, semantic, and pragmatic context in which the unit is <BR> located. Natural language has words of various sorts, nouns and verbs and <BR> such. If I say &quot;dog&quot; in isolation, you think of it as a noun, <BR> but I can also use the word in other ways. That is, a noun can function <BR> as a verb, an adjective or an adverb when the context demands it. If you <BR> dog a dog during the dog days of summer, you'll be a dog tired dogcatcher.[5] <BR> </P> <BR> <BR> <BLOCKQUOTE class=footnote> <BR> <P>[5] And you're probably dog tired of all this linguistics claptrap. <BR> But we'd like you to understand why Perl is different from the typical <BR> computer language, doggone it! </P> <BR> </BLOCKQUOTE> <BR> <BR> <P>Perl also evaluates words differently in various contexts. We will see <BR> how it does that later. Just remember that Perl is trying to understand <BR> what you're saying, like any good listener does. Perl works pretty hard <BR> to try to keep up its end of the bargain. Just say what you mean, and Perl <BR> will usually &quot;get it&quot;. (Unless you're talking nonsense, of course--the <BR> Perl parser understands Perl a lot better than either English or Swahili.) <BR> </P> <BR> <BR> <P>But back to nouns. A noun can name a particular object, or it can name <BR> a class of objects generically without specifying which one or ones are <BR> currently being referred to. Most computer languages make this distinction, <BR> only we call the particular thing a value and the generic one a variable. <BR> A value just exists somewhere, who knows where, but a variable gets associated <BR> with one or more values over its lifetime. So whoever is interpreting the <BR> variable has to keep track of that association. That interpreter may be <BR> in your brain, or in your computer. </P> <BR> <BR> <H3><A NAME="PERL2-CH-1-SECT-2.1"></A>Nouns</H3> <BR> <BR> <P><A NAME="CH01.NOUNS"></A><A NAME="CH01.VAR"></A><A NAME="CH01.NV"></A>A <BR> variable is just a handy place to keep something, a place with a name, <BR> so you know where to find your special something when you come back looking <BR> for it later. As in real life, there are various kinds of places to store <BR> things, some of them rather private, and some of them out in public. Some <BR> places are temporary, and other places are more permanent. Computer scientists <BR> love to talk about the &quot;scope&quot; of variables, but that's all they <BR> mean by it. Perl has various handy ways of dealing with scoping issues, <BR> which you'll be happy to learn later when the time is right. Which is not <BR> yet. (Look up the adjectives &quot;local&quot; and &quot;my&quot; in <A HREF="ch03_01.htm">Chapter <BR> 3, <I>Functions</I></A>, when you get curious.) </P> <BR> <BR> <P>But a more immediately useful way of classifying variables is by what <BR> sort of data they can hold. As in English, Perl's primary type distinction <BR> is between singular and plural data. Strings and numbers are singular pieces <BR> of data, while lists of strings or numbers are plural. (And when we get <BR> to object-oriented programming, you'll find that an object looks singular <BR> from the outside, but may look plural from the inside, like a class of <BR> students.) We call a singular variable a <I>scalar</I>, and a plural variable <BR> an <I>array</I>. Since a string can be stored in a scalar variable, we <BR> might write a slightly longer (and commented) version of our first example <BR> like this: </P> <BR> <BR> <PRE>$phrase = &quot;Howdy, world!\n&quot;; # Set a variable. <BR> print $phrase; # Print the variable. <BR> </PRE> <BR> <BR> <P>Note that we did not have to predefine what kind of variable <TT>$phrase</TT> <BR> is. The <TT>$</TT> character tells Perl that <TT>phrase</TT> is a scalar <BR> variable, that is, one containing a singular value. An array variable, <BR> by contrast, would start with an <TT>@</TT> character. (It may help you <BR> to remember that a <TT>$</TT> is a stylized &quot;S&quot;, for &quot;scalar&quot;, <BR> while <TT>@</TT> is a stylized &quot;a&quot;, for &quot;array&quot;.) </P> <BR> <BR> <P>Perl has some other variable types, with unlikely names like &quot;hash&quot;, <BR> &quot;handle&quot;, and &quot;typeglob&quot;. Like scalars and arrays, <BR> these types of variables are also preceded by funny characters.[6] For <BR> completeness, <A HREF="ch01_02.htm#PERL2-CH-1-TAB-1">Table 1.1</A> lists <BR> all the funny characters you'll encounter. </P> <BR> <BR> <BLOCKQUOTE class=footnote> <BR> <P>[6] Some language purists point to these funny characters as a reason <BR> to abhor Perl. This is superficial. These characters have many benefits: <BR> Variables can be interpolated into strings with no additional syntax. Perl <BR> scripts are easy to read (for people who have bothered to learn Perl!) <BR> because the nouns stand out from verbs, and new verbs can be added to the <BR> language without breaking old scripts. (We told you Perl was designed to <BR> evolve.) And the noun analogy is not frivolous--there is ample precedent <BR> in various natural languages for requiring grammatical noun markers. It's <BR> how we think! (We think.) </P> <BR> </BLOCKQUOTE> <BR> <BR> <TABLE> <BR> <CAPTION> <BR> <P><A NAME="PERL2-CH-1-TAB-1"></A>Table 1.1: Variable Syntax</P> <BR> </CAPTION> <BR> <BR> <TR CLASS=row> <BR> <TH ALIGN="left">Type</TH> <BR> <BR> <TH ALIGN="left">Character</TH> <BR> <BR> <TH ALIGN="left">Example</TH> <BR> <BR> <TH ALIGN="left">Is a name for:</TH> <BR> </TR> <BR> <BR> <TR CLASS=row> <BR> <TD ALIGN="left">Scalar</TD> <BR> <BR> <TD ALIGN="left"><TT>$</TT></TD> <BR> <BR> <TD ALIGN="left"><TT>$cents</TT></TD> <BR> <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -