📄 ch2.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 2 -- A Brief Introduction to Perl</TITLE>
<META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 2</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>A Brief Introduction to Perl</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#RunningPerl" >Running Perl</A>
<LI><A HREF="#VariablesinPerl" >Variables in Perl</A>
<UL>
<LI><A HREF="#SpecialVariables" >Special Variables</A>
<LI><A HREF="#CodeBlocks" >Code Blocks</A>
<LI><A HREF="#QuotingRules" >Quoting Rules</A>
</UL>
<LI><A HREF="#ArraysandAssociativeArrays" >Arrays and Associative Arrays</A>
<UL>
<LI><A HREF="#WhatAreAssociativeArrays" >What Are Associative Arrays?</A>
</UL>
<LI><A HREF="#ArrayOperations" >Array Operations</A>
<LI><A HREF="#FileHandlesandOperators" >File Handles and Operators</A>
<LI><A HREF="#WorkingwithPatterns" >Working with Patterns</A>
<LI><A HREF="#Subroutines" >Subroutines</A>
<LI><A HREF="#AFinalNote" >A Final Note</A>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
This chapter offers a very brief introduction to Perl programming
and syntax. If this is the first time you are working with Perl,
do not despair at the barrage of information in this chapter.
As you progress through the book, any new or elaborate syntax
will be explained. This chapter is intended as an introduction
to Perl, not a complete tutorial-you'll learn more about the advanced
features of Perl in the subsequent chapters. If you are already
familiar with Perl, you might want to glance through this chapter
to get a quick overview of the syntax and reserved words.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Note </B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
Please refer to the inside front cover for a quick reference of all the special variables in Perl. </BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="RunningPerl"><FONT SIZE=5 COLOR=#FF0000>Running Perl</FONT></A>
</H2>
<P>
Perl is a program just like any other program on your system,
only it's more powerful than most other programs! To run Perl,
you can simply type <TT><B><FONT FACE="Courier">perl</FONT></B></TT>
at the prompt and then type your code. In almost all cases, you'll
want to keep your Perl code in files just like shell scripts.
A Perl program is referred to as a script.
<P>
Normally, the Perl program on your machine will be located in
the <TT><FONT FACE="Courier">/usr/bin</FONT></TT>, <TT><FONT FACE="Courier">/usr/bin/perl5</FONT></TT>,
or <TT><FONT FACE="Courier">/usr/local/bin/perl5</FONT></TT> directory.
Use a <TT><FONT FACE="Courier">find</FONT></TT> command to see
whether you can locate Perl on your system. If you are certain
that you do not have Perl on your system, turn to <A HREF="ch24.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch24.htm" >Chapter 24</A>,
"Building and Installing the Perl 5 Interpreter," for
information on how to install Perl on your machine. Perl scripts
are of the following form:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#!/usr/bin/perl <BR>
... <I>insert code here</I> ...<BR>
# comments are text after the # mark.<BR>
#
comments can begin anywhere on a line.</FONT></TT>
</BLOCKQUOTE>
<P>
Here's a simple Perl script:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#/usr/bin/perl<BR>
print "\n Whoa! That was good!\n";</FONT></TT>
</BLOCKQUOTE>
<P>
If the path to the Perl program on your system is different, you'll
have to use that pathname instead of <TT><FONT FACE="Courier">/usr/bin/perl</FONT></TT>.
You also can specify programs on the command line with the <TT><FONT FACE="Courier">-e</FONT></TT>
switch to Perl. For example, entering the following command at
the prompt will print <TT><FONT FACE="Courier">Howdy!</FONT></TT>.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$ perl -e 'print "Howdy !\n";'</FONT></TT>
</BLOCKQUOTE>
<P>
In all but the shortest of Perl programs, you'll use a file to
store your Perl code as a script. Using a script file offers you
the ease of not having to type all the commands interactively
and thus not being able to correct typing errors easily. Also,
a script file provides a written record of what commands to use
to accomplish a certain task.
<P>
To fire off a command on all lines in the input, use <TT><FONT FACE="Courier">-n</FONT></TT>
option. Thus, the line
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$perl -n 's/old/new/g' test.txt</FONT></TT>
</BLOCKQUOTE>
<P>
runs the command to substitute all strings <TT><FONT FACE="Courier">old</FONT></TT>
with <TT><FONT FACE="Courier">new</FONT></TT> on each line from
the file <TT><FONT FACE="Courier">test.txt</FONT></TT>. If you
use the <TT><FONT FACE="Courier">-p</FONT></TT> option, it prints
each line as it is read in. The <TT><FONT FACE="Courier">-v</FONT></TT>
option prints the version number of Perl you are running. This
book is written for Perl 5.002.
<P>
Now, let's begin the introduction to the Perl language.
<H2><A NAME="VariablesinPerl"><FONT SIZE=5 COLOR=#FF0000>Variables
in Perl</FONT></A></H2>
<P>
Perl has three basic types of variables: scalars, arrays, and
associative arrays. A <I>scalar </I>variable is anything that
can hold one number (either as a floating point number or as an
integer) or a string. An <I>array</I> stores many scalars in a
sequence, where each scalar can be indexed using a number starting
with 0 on up. An <I>associative array</I> is like an array in
that it stores strings in sequence but uses another string as
an index to address individual items instead of a number. I cover
how to use these three types of variables in this chapter.
<P>
The syntax for a scalar variable is <TT><FONT FACE="Courier">$variable_name</FONT></TT>.
A variable name is set up and addressed in the same way as Bourne
shell variables. To assign values to a scalar, you use statements
like these:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$name = "Kamran";<BR>
$number= 100;<BR>
$phone_Number = '555-1232';</FONT></TT>
</BLOCKQUOTE>
<P>
A variable in Perl is evaluated at runtime to derive a value that
is one of the following: a string, a number, or a pointer to scalar.
(To see the use of pointers and references, refer to <A HREF="ch3.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch3.htm" >Chapter 3</A>,
"References.")
<P>
To print out the value of a variable, you use a <TT><FONT FACE="Courier">print</FONT></TT>
statement. Therefore, to print the value of <TT><FONT FACE="Courier">$name</FONT></TT>,
you would make the following call:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">print $name;</FONT></TT>
</BLOCKQUOTE>
<P>
The value of <TT><FONT FACE="Courier">$name</FONT></TT> is printed
to the screen. Perl scripts "expect" input from a standard
input (the keyboard) and to write to the standard output. Of course,
you can also use the <TT><FONT FACE="Courier">print</FONT></TT>
statement to print the values of special variables that are built
into Perl.
<H3><A NAME="SpecialVariables">Special Variables</A></H3>
<P>
Table 2.1 lists the special variables in Perl. The first column
contains the variable, and the second contains a verbose name
that you can use to make the code readable. The third column in
the table describes the contents of each variable.
<P>
You can use the verbose names (in column 2) by including the following
line in the beginning of your code:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">use English; </FONT></TT>
</BLOCKQUOTE>
<P>
This statement will let you use the <TT><FONT FACE="Courier">English.pm</FONT></TT>
module in your code. (I cover the use of modules in <A HREF="ch4.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch4.htm" >Chapter 4</A>,
"Introduction to Perl Modules.") Not all Perl variables
have an equivalent name in the <TT><FONT FACE="Courier">English.pm</FONT></TT>
module. The entry "n/a" in the second column indicates
that there is not an English name for the variable. <BR>
<P>
<CENTER><B>Table 2.1. Special variables in Perl. </B></CENTER>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=100><I>Variable</I></TD><TD WIDTH=245><I>English Name</I>
</TD><TD WIDTH=245><I>Description</I></TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$_</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$ARG</FONT></TT></TD><TD WIDTH=245>The default input and output pattern searching space
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$1-$9</FONT></TT></TD>
<TD WIDTH=245>n/a</TD><TD WIDTH=245>The subpattern from the last set of parentheses in a pattern match
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$&</FONT></TT>
</TD><TD WIDTH=245><TT><FONT FACE="Courier">$MATch</FONT></TT>
</TD><TD WIDTH=245>The last pattern matched (RO)</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$`</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$PREMATch</FONT></TT></TD>
<TD WIDTH=245>The string preceding a pattern match (RO)</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$´</FONT></TT>
</TD><TD WIDTH=245><TT><FONT FACE="Courier">$POSTMATch</FONT></TT>
</TD><TD WIDTH=245>The string following a pattern match (RO)</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$+</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$LAST_PAREN_MATch</FONT></TT>
</TD><TD WIDTH=245>The last bracket matched in a pattern (RO)
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$*</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$MULTILINE_MATchING</FONT></TT>
</TD><TD WIDTH=245>Set to <TT><FONT FACE="Courier">1</FONT></TT> to enable multi-line matching; set to <TT><FONT FACE="Courier">0</FONT></TT> by default
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$.</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$INPUT_LINE_NUMBER</FONT></TT>
</TD><TD WIDTH=245>The current input line number; reset on <TT><FONT FACE="Courier">close()</FONT></TT> call only
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$/</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$INPUT_RECORD_SEPARATOR</FONT></TT>
</TD><TD WIDTH=245>The newline by default</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$|</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$AUTO_FLUSH</FONT></TT>
</TD><TD WIDTH=245>If set to <TT><FONT FACE="Courier">1</FONT></TT>, forces a flush on every <TT><FONT FACE="Courier">write</FONT></TT> or <TT><FONT FACE="Courier">print</FONT></TT>; <TT><FONT FACE="Courier">0</FONT></TT> by default
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$,</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$OUTPUT_FIELD_SEPARATOR</FONT></TT>
</TD><TD WIDTH=245>Specifies what is printed between fields</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$\</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$INPUT_RECORD_SEPARATOR</FONT></TT>
</TD><TD WIDTH=245>The output record separator for the print operator
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$"</FONT></TT>
</TD><TD WIDTH=245><TT><FONT FACE="Courier">$LIST_SEPARATOR</FONT></TT>
</TD><TD WIDTH=245>The separator for elements within a list</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$;</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$SUBSCRIPT_SEPARATOR</FONT></TT>
</TD><TD WIDTH=245>The character for multidimensional array emulation
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$#</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$FORMAT</FONT></TT></TD>
<TD WIDTH=245>Output format for printed numbers</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$%</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$FORMAT_PAGE_NUMBER</FONT></TT>
</TD><TD WIDTH=245>The current page number</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$=</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$FORMAT_LINES_PER_PAGE</FONT></TT>
</TD><TD WIDTH=245>The number of lines per page</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$-</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$FORMAT_LINES_LEFT</FONT></TT>
</TD><TD WIDTH=245>The number of lines still left to draw on the page
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$~</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$FORMAT_NAME</FONT></TT>
</TD><TD WIDTH=245>The name of the current format being used</TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=100><TT><FONT FACE="Courier">$^</FONT></TT></TD>
<TD WIDTH=245><TT><FONT FACE="Courier">$FORMAT_TOP_NAME</FONT></TT>
</TD><TD WIDTH=245>The name of the current top-of-page format
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -