📄 ch02_02.htm
字号:
<html><head><title>Molecules (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="Molecules"><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_01.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_03.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.2. Molecules</h2><p><a name="INDEX-407"></a>Perl is a <em class="emphasis">free-form</em> language, but that doesn't mean that Perl istotally free of form. As computer folks usually use the term, afree-form language is one in which you can put spaces, tabs, andnewlines anywhere you like--except where you can't.</p><p><a name="INDEX-408"></a><a name="INDEX-409"></a> One obviousplace you can't put a whitespace character is in the middle of atoken. A <em class="emphasis">token</em> is what we call a sequence ofcharacters with a unit of meaning, much like a simple word in naturallanguage. But unlike the typical word, a token might contain othercharacters besides letters, just as long as they hang together to form aunit of meaning. (In that sense, they're more like molecules, whichdon't have to be composed of only one particular kind of atom.) Forexample, numbers and mathematical operators are considered tokens. An<em class="emphasis">identifier</em> is a token that starts with a letteror underscore and contains only letters, digits, and underscores. Atoken may not contain whitespace characters because this would splitthe token into two tokens, just as a space in an English word turns itinto two words.<a href="#FOOTNOTE-2">[2]</a></p><blockquote class="footnote"><a name="FOOTNOTE-2"></a><p>[2] The astute reader will point out thatliteral strings may contain whitespace characters. But strings canget away with it only because they have quotes on both ends to keepthe spaces from leaking out.</p></blockquote><p><a name="INDEX-410"></a><a name="INDEX-411"></a><a name="INDEX-412"></a><a name="INDEX-413"></a><a name="INDEX-414"></a>Although whitespace is allowed between any two tokens, whitespace is<em class="emphasis">required</em> only between tokens that would otherwise be confused as asingle token. All whitespace is equivalent for this purpose. Newlinesare distinguished from spaces and tabs only within quoted strings,formats, and certain line-oriented forms of quoting. Specifically,newlines do not terminate statements as they do in certain otherlanguages (such as FORTRAN or Python). Statements in Perl are terminatedwith semicolons, just as they are in C and its various derivatives.</p><p><a name="INDEX-415"></a><a name="INDEX-416"></a><a name="INDEX-417"></a><a name="INDEX-418"></a><a name="INDEX-419"></a>Unicode whitespace characters are allowed in a Unicode Perl program,but you need to be careful. If you use the special Unicode paragraphand line separators, be aware that Perl may count line numbersdifferently than your text editor does, so error messages may be moredifficult to interpret. It's best to stick with good old-fashionednewlines.</p><p><a name="INDEX-420"></a>Tokens are recognized greedily; if at a particular point the Perlparser has a choice between recognizing a short token or a long token,it will choose the long one. If you meant it to be two tokens, justinsert some whitespace between the tokens. (We tend to put extraspace around most operators anyway, just for readability.)</p><p><a name="INDEX-421"></a><a name="INDEX-422"></a> Comments are indicated by the<tt class="literal">#</tt> character and extend from there through the endof the line. A comment counts as whitespace for separating tokens.The Perl language attaches no special meaning to anything you mightput into a comment.<a href="#FOOTNOTE-3">[3]</a></p><blockquote class="footnote"><a name="FOOTNOTE-3"></a><p>[3] Actually, that's a small fib. ThePerl parser does look for command-line switches on an initial<tt class="literal">#!</tt> line (see<a href="ch19_01.htm">Chapter 19, "The Command-Line Interface"</a>). It can also interpretthe line number directives that various preprocessors produce (see thesection <a href="ch24_05.htm#ch24-sect-gen">Section 2.5.2, "Generating Perl in Other Languages"</a> in <a href="ch24_01.htm">Chapter 24, "Common Practices"</a>).</p></blockquote><p><a name="INDEX-423"></a><a name="INDEX-424"></a>One other oddity is that if a line begins with <tt class="literal">=</tt>anywhere a statement would be legal,Perl ignores everything from that line down to the next line thatbegins with <tt class="literal">=cut</tt>. The ignored text is assumed tobe <em class="emphasis">pod</em>, or "plain old documentation". The Perldistribution has programs that will extract pod commentary from Perlmodules and turn it into flat text, manpages, [LaTeX], HTML, or (somedaysoon) XMLdocuments. In a complementary fashion, the Perl parser extracts thePerl code from Perl modules and ignores the pod. So you may considerthis an alternate, multiline form of commenting. You may alsoconsider it completely nuts, but Perl modules documented this waynever lose track of their documentation. See <a href="ch26_01.htm">Chapter 26, "Plain Old Documentation"</a>, for details on pod,including a description of how to effect multiline comments in Perl.</p><p>But don't look down on the normal comment character. There's somethingcomforting about the visual effect of a nice row of <tt class="literal">#</tt> charactersdown the left side of a multiline comment. It immediately tells youreyes: "This is not code." You'll note that even in languages with amultiline quoting mechanisms like C, people often put a row of <tt class="literal">*</tt>characters down the left side of their comments anyway. Appearances areoften more important than they appear.</p><p><a name="INDEX-425"></a>In Perl, just as in chemistry and in language, you can build largerand larger structures out of the smaller ones. We already mentionedthe <em class="emphasis">statement</em>; it's just a sequence of tokensthat make up a command, that is, a sentence in the imperative mood.You can combine a sequence of statements into a<em class="emphasis">block</em> that is delimited by braces (also knownaffectionately as "curlies" by people who confuse braces withsuspenders.) Blocks can in turn be combined into larger blocks. Someblocks function as <em class="emphasis">subroutines</em>, which can becombined into <em class="emphasis">modules</em>, which can be combined into<em class="emphasis">programs</em>. But we're getting ahead ofourselves--those are subjects for coming chapters. Let's build somemore tokens out of characters.</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="ch02_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">2.1. Atoms</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.3. Built-in Data Types</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 + -