📄 ch29.htm
字号:
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<UL>
<LI><A HREF="#Heading1">- 29 -</A>
<UL>
<LI><A HREF="#Heading2">Perl</A>
<UL>
<LI><A HREF="#Heading3">What Is Perl?</A>
<LI><A HREF="#Heading4">How Do I Find Perl?</A>
<UL>
<LI><A HREF="#Heading5">Where Do I Get Perl?</A>
<LI><A HREF="#Heading6">Other Places to Get Perl</A>
</UL>
<LI><A HREF="#Heading7">A Sample Perl Program</A>
<LI><A HREF="#Heading8">Listing 29.1. A simple Perl program that reads and writes
a line of input.</A>
<LI><A HREF="#Heading9">Running a Perl Program</A>
<UL>
<LI><A HREF="#Heading10">If Something Goes Wrong</A>
</UL>
<LI><A HREF="#Heading11">Line 1 of Your Program: How Comments Work</A>
<LI><A HREF="#Heading12">NOTE</A>
<UL>
<LI><A HREF="#Heading13">Comments</A>
</UL>
<LI><A HREF="#Heading14">NOTE</A>
<LI><A HREF="#Heading15">Listing 29.2. A simple Perl program with comments.</A>
<LI><A HREF="#Heading16">NOTE</A>
<LI><A HREF="#Heading17">Line 2: Statements, Tokens, and <STDIN></A>
<UL>
<LI><A HREF="#Heading18">Statements and Tokens</A>
<LI><A HREF="#Heading19">Tokens and White Space</A>
<LI><A HREF="#Heading20">What the Tokens Do When Reading from Standard Input</A>
</UL>
<LI><A HREF="#Heading21">NOTE</A>
<LI><A HREF="#Heading22">Line 3: Writing to Standard Output</A>
<UL>
<LI><A HREF="#Heading23">Function Invocations and Arguments</A>
</UL>
<LI><A HREF="#Heading24">Error Messages</A>
<LI><A HREF="#Heading25">Listing 29.3. A program containing an error.</A>
<LI><A HREF="#Heading26">TIP</A>
<LI><A HREF="#Heading27">Interpretive Languages Versus Compiled Languages</A>
<LI><A HREF="#Heading28">Summary</A>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 29 -</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">Perl</FONT></H2>
<P><I>by David Till</I></P>
<P>IN THIS CHAPTER</P>
<UL>
<LI>What Is Perl?
<P>
<LI>How Do I Find Perl?
<P>
<LI>A Sample Perl Program
<P>
<LI>Running a Perl Program
<P>
<LI>Line 1 of Your Program: How Comments Work
<P>
<LI>Line 2: Statements, Tokens, and <STDIN>
<P>
<LI>Line 3: Writing to Standard Output
<P>
<LI>Error Messages
<P>
<LI>Interpretive Languages Versus Compiled Languages
</UL>
<P><BR>
Welcome to a brief look at Perl 5. In this chapter, you'll learn about the following
topics:
<UL>
<LI>What Perl is and why Perl is useful
<P>
<LI>How to get Perl if you do not already have it
<P>
<LI>How to run Perl programs
<P>
<LI>How to write a simple Perl program
<P>
<LI>The difference between interpretive and compiled programming languages
<P>
<LI>What an algorithm is and how to develop one
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading3<FONT COLOR="#000077">What Is Perl?</FONT></H3>
<P>Perl is an acronym, short for Practical Extraction and Report Language. It was
designed by Larry Wall as a tool for writing programs in the UNIX environment and
is continually being updated and maintained by him.</P>
<P>For its many fans, Perl provides the best of several worlds. For instance:
<UL>
<LI>Perl has the power and flexibility of a high-level programming language such
as C. In fact, as you will see, many of the features of the language are borrowed
from C.
<P>
<LI>Like shell script languages, Perl does not require a special compiler and linker
to turn the programs you write into working code. Instead, all you have to do is
write the program and tell Perl to run it. This means that Perl is ideal for producing
quick solutions to small programming problems, or for creating prototypes to test
potential solutions to larger problems.
<P>
<LI>Perl provides all the features of the script languages sed and awk, plus features
not found in either of these two languages. Perl also supports a sed-to-Perl translator
and an awk-to-Perl translator.
</UL>
<P>In short, Perl is as powerful as C but as convenient as awk, sed, and shell scripts.</P>
<P>As you'll see, Perl is very easy to learn. Indeed, if you are familiar with other
programming languages, learning Perl is a snap. Even if you have very little programming
experience, Perl can have you writing useful programs in a very short time. If you
pick up a copy of Teach Yourself Perl 5 in 21 Days (Sams Publishing, 1995), you'll
easily learn enough about Perl to be able to solve many problems.
<H3 ALIGN="CENTER"><A NAME="Heading4<FONT COLOR="#000077">How Do I Find Perl?</FONT></H3>
<P>To find out whether Perl already is available on your system, take the following
steps:
<UL>
<P>If you are currently working in a UNIX programming environment, check to see whether
the file <TT>/usr/local/bin/perl</TT> exists.
<LI>
<LI>If you are working in any other environment, check the place where you normally
keep your executable programs, or check the directories accessible from your <TT>PATH</TT>
environment variable.
</UL>
<P>If you do not find Perl in this way, talk to your system administrator and ask
whether he has Perl running somewhere else. If you don't have Perl running in your
environment, don't despair--read on!
<H4 ALIGN="CENTER"><A NAME="Heading5<FONT COLOR="#000077">Where Do I Get Perl?</FONT></H4>
<P>One of the reasons Perl is becoming so popular is that it is available free to
anyone who wants it. If you are on the Internet, you can obtain a copy of Perl with
File Transfer Protocol (FTP). Following is a sample FTP session that transfers a
copy of the Perl distribution. The items shown in boldface type are what you would
enter during the session.</P>
<PRE><FONT COLOR="#0066FF">$ ftp prep.ai.mit.edu
Connected to prep.ai.mit.edu.
220 aeneas FTP server (Version wu-2.4(1) Thu Apr 14 20:21:35 EDT 1994)
Âready.
Name (prep.ai.mit.edu:dave): anonymous
331 Guest login ok, send your complete e-mail address as password.
Password:
230-Welcome, archive user!
230-
230-If you have problems downloading and are seeing "Access denied" or
230-"Permission denied", please make sure that you started your FTP
230-client in a directory to which you have write permission.
230-
230-If you have any problems with the GNU software or its downloading,
230-please refer your questions to <gnu@PREP.AI.MIT.EDU>. If you have any
230-other unusual problems, please report them to <root@aeneas.MIT.EDU>.
230-
230-If you do have problems, please try using a dash (-) as the first
230-character of your password -- this will turn off the continuation
230-messages that may be confusing your FTP client.
230-
230 Guest login ok, access restrictions apply.
ftp> cd pub/gnu
250-If you have problems downloading and are seeing "Access denied" or
250-"Permission denied", please make sure that you started your FTP
250-client in a directory to which you have write permission.
250-
250-Please note that all files ending in `.gz' are compressed with
250-'gzip', not with the unix `compress' program. Get the file README
250- and read it for more information.
250-
250-Please read the file README
250- it was last modified on Thu Feb 1 15:00:50 1996 - 32 days ago
250-Please read the file README-about-.diff-files
250- it was last modified on Fri Feb 2 12:57:14 1996 - 31 days ago
250-Please read the file README-about-.gz-files
250- it was last modified on Wed Jun 14 16:59:43 1995 - 264 days ago
250 CWD command successful.
ftp> binary
200 Type set to I.
ftp> get perl-5.001.tar.gz
200 PORT command successful.
150 Opening ASCII mode data connection for perl-5.001.tar.gz (1130765 bytes).
226 Transfer complete.
1130765 bytes received in 9454 seconds (1.20 Kbytes/s)
ftp> quit
221 Goodbye.
$
</FONT></PRE>
<P>The commands entered in this session are explained in the following steps. If
some of these steps are not familiar to you, ask your system administrator for help.
<DL>
<DD><B>1.</B> The command <TT>$ ftp prep.ai.mit.edu</TT> connects you to the main
Free Software Foundation source depository at MIT.<BR>
<BR>
<B>2.</B> The user ID <TT>anonymous</TT> tells FTP that you want to perform an anonymous
FTP operation.<BR>
<BR>
<B>3.</B> When FTP asks for a password, enter your user ID and network address. This
lets the MIT system administrator know who is using the MIT archives. (For security
reasons, the password is not actually displayed when you type it.)<BR>
<BR>
<B>4.</B> The command <TT>cd pub/gnu</TT> sets your current working directory to
be the directory containing the Perl source.<BR>
<BR>
<B>5.</B> The <TT>binary</TT> command tells FTP that the file you'll be receiving
is a file that contains unreadable (nontext) characters.<BR>
<BR>
<B>6.</B> The <TT>get</TT> command copies the file <TT>perl-5.001.tar.gz</TT> from
the MIT source depository to your own site. (It's usually best to do this in off-peak
hours to make things easier for other Internet users--it takes a while.) This file
is quite large because it contains all the source files for Perl bundled together
into a single file.<BR>
<BR>
<B>7.</B> The <TT>quit</TT> command disconnects from the MIT source repository and
returns you to your own system.
</DL>
<P>After you've retrieved the Perl distribution, take the following steps:
<DL>
<DD><B>1.</B> Create a directory and move the file you just received, <TT>perl-5.001.tar.gz</TT>,
to this directory. (Or, alternatively, move it to a directory already reserved for
this purpose.)<BR>
<BR>
<B>2. </B>The <TT>perl-5.001.tar.gz</TT> file is compressed to save space. To uncompress
it, enter this command:
</DL>
<PRE><FONT COLOR="#0066FF">$ gunzip perl-5.001.tar.gz
</FONT></PRE>
<DL>
<DD><B>1.</B> gunzip is the GNU uncompress program. If it's not available on your
system, see your system administrator. (You can, in fact, retrieve it from <TT>prep.ai.mit.edu</TT>
using anonymous FTP with the same commands you used to retrieve the Perl distribution.)<BR>
<BR>
<B>2.</B> When you run gunzip, the file <TT>perl-5.001.tar.gz</TT> will be replaced
by <TT>perl-5.001.tar</TT>, which is the uncompressed version of the Perl distribution
file.<BR>
<BR>
<B>3.</B> The next step is to unpack the Perl distribution. In other words, use the
information in the Perl distribution to create the Perl source files. To do this,
enter the following command:
</DL>
<PRE><FONT COLOR="#0066FF">$ tar xvf - <perl-5.001.tar
</FONT></PRE>
<DL>
<DD>As this command executes, it creates each source file in turn and displays the
name and size of each file as it is created. The <TT>tar</TT> command also creates
subdirectories where appropriate; this ensures that the Perl source files are organized
in a logical way.<BR>
<BR>
<B>4.</B> Using your favorite C compiler, compile the Perl source code using the
makefile provided. (This makefile should have been created when the source files
were unpacked in the preceding step.)<BR>
<BR>
<B>5.</B> Place the compiled Perl executable into the directory where you normally
keep your executables. On UNIX systems, this directory usually is called <TT>/usr/local/bin</TT>,
and Perl usually is named <TT>/usr/local/bin/perl</TT>.
</DL>
<P>You might need your system administrator's help to do this because you might not
have the necessary permissions.
<H4 ALIGN="CENTER"><A NAME="Heading6<FONT COLOR="#000077">Other Places to Get
Perl</FONT></H4>
<P>If you cannot access the MIT site from where you are, you can get Perl from the
following sites via anonymous FTP:</P>
<CENTER>
<P><FONT SIZE="4"><B>North America </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><I>Site</I>
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><I>Location</I>
</BLOCKQUOTE>
<P>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>ftp.netlabs.com</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>192.94.48.152</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Directory <TT>/pub/outgoing/perl5.0</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>ftp.cis.ufl.edu</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>128.227.100.198</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Directory <TT>/pub/perl/src/5.0</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>ftp.uu.net</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>192.48.96.9</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Directory <TT>/languages/perl</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>ftp.khoros.unm.edu</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>198.59.155.28</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Directory <TT>/pub/perl</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>ftp.cbi.tamucc.edu</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>165.95.1.3</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Directory <TT>/pub/duff/Perl</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>ftp.metronet.com</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>192.245.137.1</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Directory <TT>/pub/perl/sources</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><TT>genetics.upenn.edu</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -