📄 ch04_01.htm
字号:
<html><head><title>The Perl Language (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 & 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="ch03_05.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 4. The Perl Language</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4> <p> <a href="#perlnut2-CHP-4-SECT-1">Program Structure</a><br /><a href="ch04_02.htm">Data Types and Variables</a><br /><a href="ch04_03.htm">Statements</a><br /><a href="ch04_04.htm">Special Variables</a><br /><a href="ch04_05.htm">Operators</a><br /><a href="ch04_06.htm">Regular Expressions</a><br /><a href="ch04_07.htm">Subroutines</a><br /><a href="ch04_08.htm">References and Complex Data Structures</a><br /><a href="ch04_09.htm">Filehandles</a><br /><a href="ch04_10.htm">Signals</a><br /><a href="ch04_11.htm">Unicode</a><br /><a href="ch04_12.htm">Formats</a><br /><a href="ch04_13.htm">Pod</a><br /></p></div><p>This chapter is a quick and merciless guide to the Perl languageitself. If you're trying to learn Perl from scratchand would prefer to be taught rather than to have things thrown atyou, then you might be better off with <em class="emphasis">Learning Perl, 3rdEdition</em> by Randal L. Schwartz and Tom Phoenix. However, ifyou already know some other programming languages and just want tolearn the particulars of Perl, this chapter is for you. Sit tight,and forgive us for being terse—we have a lot of ground tocover.</p><p>If you want a more complete discussion of the Perl language and itsidiosyncrasies (and we mean <em class="emphasis">complete</em>), see<em class="emphasis">Programming Perl, 3rd Edition</em> by Larry Wall, TomChristiansen, and Jon Orwant.</p><div class="sect1"><a name="perlnut2-CHP-4-SECT-1" /></a><h2 class="sect1">4.1. Program Structure</h2><p><a name="INDEX-205" /></a><a name="INDEX-206" /></a>Perl is a particularly forgivinglanguage, as far as program layout goes. There are no rules aboutindentation, newlines, etc. Most lines end with semicolons, but noteverything has to. Most things don't have to bedeclared, except for a couple of things that do. Here are the bareessentials:</p><dl><dt><i><em class="emphasis">Whitespace</em></i></dt><dd><a name="INDEX-207" /></a>Whitespace isrequired only between items that would otherwise be confused as asingle term. All types of whitespace—spaces, tabs, newlines,etc.—are equivalent in this context. A comment counts aswhitespace. Different types of whitespace are distinguishable withinquoted strings, formats, and certain line-oriented forms of quoting.For example, in a quoted string, a newline, a space, and a tab areinterpreted as unique characters.<p></p></dd><dt><i><em class="emphasis">Semicolons</em></i></dt><dd><a name="INDEX-208" /></a><a name="INDEX-209" /></a>Every simple statement must endwith a semicolon. Compound statements contain brace-delimited blocksof other statements and do not require terminating semicolons afterthe ending brace. A final simple statement in a block also does notrequire a semicolon.<p></p></dd><dt><i><em class="emphasis">Declarations</em></i></dt><dd><a name="INDEX-210" /></a>Onlysubroutines and report formats need to be explicitly declared. Allother user-created objects are automatically created with a null or 0value unless they are defined by some explicit operation such asassignment. The <em class="emphasis">-w</em> command-line switch will warnyou about using undefined values.<p></p><p>You may force yourself to declare your variables by including the<tt class="literal">use strict</tt> pragma in your programs (see <a href="ch08_01.htm">Chapter 8, "Standard Modules"</a> for more information on pragmas and<tt class="literal">strict</tt> in particular). This causes an error if youdo not explicitly declare your variables.</p></dd><dt><i> <em class="emphasis">Comments and documentation</em></i></dt><dd><a name="INDEX-211" /></a><a name="INDEX-212" /></a><a name="INDEX-213" /></a>Comments within a program areindicated by a pound sign (<tt class="literal">#</tt>). Everythingfollowing a pound sign to the end of the line is interpreted as acomment.<p></p><p>Lines starting with <tt class="literal">=</tt> are interpreted as the<a name="INDEX-214" /></a><a name="INDEX-215" /></a><a name="INDEX-216" /></a><a name="INDEX-217" /></a><a name="INDEX-218" /></a>start of asection of embedded documentation (pod), and all subsequent linesuntil the next <tt class="literal">=cut</tt> are ignored by the compiler.See <a href="ch04_12.htm#perlnut2-CHP-4-SECT-12">Section 4.12, "Formats"</a> laterin this chapter for more information on pod format.</p></dd></dl></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch04_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">3.5. Threads</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">4.2. Data Types and Variables</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><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 + -