?? unx16.htm
字號:
<HTML>
<HEAD>
<TITLE>UNIX Unleashed unx16.htm</TITLE>
<LINK REL="ToC" HREF="index.htm">
<LINK REL="Next" HREF="unx17.htm">
<LINK REL="Previous" HREF="unx15.htm"></HEAD>
<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>
<P><A HREF="unx15.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="unx17.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>
</P><UL>
<LI>
<A HREF="#I1">16</A></LI>
<UL>
<LI>
<A HREF="#I3">Perl</A></LI>
<UL>
<UL>
<UL>
<LI>
<A HREF="#I4">By David Till</A></LI></UL></UL>
<LI>
<A HREF="#I5">Overview</A></LI>
<UL>
<LI>
<A HREF="#I6">What Is Perl?</A></LI>
<LI>
<A HREF="#I7">Where Can I Get Perl?</A></LI>
<LI>
<A HREF="#I8">A Simple Sample Program</A></LI>
<LI>
<A HREF="#I9">Using Comments</A></LI>
<LI>
<A HREF="#I10">Reading from Standard Input</A></LI>
<LI>
<A HREF="#I11">Storing Values The Scalar Variable</A></LI>
<LI>
<A HREF="#I12">Assigning a Value to a Scalar Variable</A></LI>
<LI>
<A HREF="#I13">Scalar Variables Inside Character Strings</A></LI>
<LI>
<A HREF="#I14">Writing to Standard Output</A></LI></UL>
<LI>
<A HREF="#I15">Working with Scalar Variables</A></LI>
<UL>
<LI>
<A HREF="#I16">Understanding Scalar Values</A></LI>
<UL>
<LI>
<A HREF="#I17">Using Octal and Hexadecimal Representation</A></LI>
<LI>
<A HREF="#I18">Using Double- and Single-Quoted Strings</A></LI>
<LI>
<A HREF="#I19">Using Floating-Point Values</A></LI>
<LI>
<A HREF="#I20">Interchangeability of Strings and Numeric Values</A></LI></UL>
<LI>
<A HREF="#I21">Using Scalar Variable Operators</A></LI>
<UL>
<LI>
<A HREF="#I22">Performing Arithmetic</A></LI>
<LI>
<A HREF="#I23">Performing Comparisons</A></LI>
<LI>
<A HREF="#I24">Manipulating Bits</A></LI>
<LI>
<A HREF="#I25">Using the Assignment Operators</A></LI>
<LI>
<A HREF="#I26">Using Autoincrement and Autodecrement</A></LI>
<LI>
<A HREF="#I27">Concatenating and Repeating Strings</A></LI>
<LI>
<A HREF="#I28">Using Other C Operators</A></LI>
<LI>
<A HREF="#I29">Matching Patterns</A></LI>
<LI>
<A HREF="#I30">The Order of Operations</A></LI></UL></UL>
<LI>
<A HREF="#I31">Using Lists and Array Variables</A></LI>
<UL>
<LI>
<A HREF="#I32">Introducing Lists</A></LI>
<UL>
<LI>
<A HREF="#I33">Scalar Variables and Lists</A></LI>
<LI>
<A HREF="#I34">Using List Ranges</A></LI></UL>
<LI>
<A HREF="#I35">Storing Lists in Array Variables</A></LI>
<LI>
<A HREF="#I36">Assigning to Array Variables</A></LI>
<UL>
<LI>
<A HREF="#I37">Using Array Variables in Lists</A></LI>
<LI>
<A HREF="#I38">Assigning to Scalar Variables from Array Variables</A></LI>
<LI>
<A HREF="#I39">Retrieving the Length of a List</A></LI></UL>
<LI>
<A HREF="#I40">Using Array Slices</A></LI>
<UL>
<LI>
<A HREF="#I41">Using Array Slices with Scalar Variables</A></LI>
<LI>
<A HREF="#I42">Using the Array Slice Notation as a Shorthand</A></LI></UL>
<LI>
<A HREF="#I43">Other Array Operations</A></LI>
<UL>
<LI>
<A HREF="#I44">Sorting a List or Array Variable</A></LI>
<LI>
<A HREF="#I45">Reversing a List or Array Variable</A></LI>
<LI>
<A HREF="#I46">Using chop() on Array Variables</A></LI>
<LI>
<A HREF="#I47">Creating a Single String from a List</A></LI>
<LI>
<A HREF="#I48">Example: Sorting Words in a String</A></LI></UL>
<LI>
<A HREF="#I49">Using Command Line Arguments</A></LI>
<LI>
<A HREF="#I50">Standard Input and Array Variables</A></LI></UL>
<LI>
<A HREF="#I51">Controlling Program Flow</A></LI>
<UL>
<LI>
<A HREF="#I52">Conditional Execution The if Statement</A></LI>
<LI>
<A HREF="#I53">Two-Way Branching Using if and else</A></LI>
<UL>
<LI>
<A HREF="#I54">Multi-Way Branching Using elsif</A></LI></UL>
<LI>
<A HREF="#I55">Conditional Branching Using unless</A></LI>
<LI>
<A HREF="#I56">Repeating Statements Using while and until</A></LI>
<LI>
<A HREF="#I57">Using Single-Line Conditional Statements</A></LI>
<LI>
<A HREF="#I58">Looping with the for Statement</A></LI>
<LI>
<A HREF="#I59">Looping Through a List The foreach Statement</A></LI>
<LI>
<A HREF="#I60">Exiting a Loop with the last Statement</A></LI>
<LI>
<A HREF="#I61">Using next to Start the Next Iteration of a Loop</A></LI>
<LI>
<A HREF="#I62">Using Labelled Blocks for Multi-Level Jumps</A></LI>
<LI>
<A HREF="#I63">Terminating Execution Using die</A></LI></UL>
<LI>
<A HREF="#I64">Reading from and Writing to Files</A></LI>
<UL>
<LI>
<A HREF="#I65">Opening a File</A></LI>
<UL>
<LI>
<A HREF="#I66">Checking Whether the Open Succeeded</A></LI></UL>
<LI>
<A HREF="#I67">Reading from a File</A></LI>
<LI>
<A HREF="#I68">Writing to a File</A></LI>
<LI>
<A HREF="#I69">Closing a File</A></LI>
<LI>
<A HREF="#I70">Determining the Status of a File</A></LI>
<LI>
<A HREF="#I71">Reading from a Sequence of Files</A></LI></UL>
<LI>
<A HREF="#I72">Using Subroutines</A></LI>
<UL>
<LI>
<A HREF="#I73">Defining a Subroutine</A></LI>
<LI>
<A HREF="#I74">Using a Subroutine</A></LI>
<LI>
<A HREF="#I75">Returning a Value from a Subroutine</A></LI>
<LI>
<A HREF="#I76">Using Local Variables</A></LI>
<LI>
<A HREF="#I77">Passing Values to a Subroutine</A></LI>
<UL>
<LI>
<A HREF="#I78">Calling Subroutines from Other Subroutines</A></LI></UL></UL>
<LI>
<A HREF="#I79">Associative Arrays</A></LI>
<UL>
<LI>
<A HREF="#I80">Defining Associative Arrays</A></LI>
<LI>
<A HREF="#I81">Accessing Associative Arrays</A></LI>
<LI>
<A HREF="#I82">Copying to and from Associative Arrays</A></LI>
<LI>
<A HREF="#I83">Adding and Deleting Array Elements</A></LI>
<LI>
<A HREF="#I84">Listing Array Indexes and Values</A></LI>
<LI>
<A HREF="#I85">Looping with an Associative Array</A></LI></UL>
<LI>
<A HREF="#I86">Formatting Your Output</A></LI>
<UL>
<LI>
<A HREF="#I87">Defining a Print Format</A></LI>
<LI>
<A HREF="#I88">Displaying a Print Format</A></LI>
<LI>
<A HREF="#I89">Displaying Values in a Print Format</A></LI>
<LI>
<A HREF="#I90">Choosing a Value Field Format</A></LI>
<LI>
<A HREF="#I91">Writing to Other Output Files</A></LI>
<LI>
<A HREF="#I92">Specifying a Page Header</A></LI>
<UL>
<LI>
<A HREF="#I93">Setting the Page Length</A></LI></UL>
<LI>
<A HREF="#I94">Formatting Long Character Strings</A></LI></UL>
<LI>
<A HREF="#I95">Using Built-In Functions</A></LI>
<UL>
<LI>
<A HREF="#I96">The $_ Variable</A></LI></UL>
<LI>
<A HREF="#I97">Summary</A></LI></UL></UL></UL>
<H1 ALIGN="CENTER">
<CENTER><A ID="I1" NAME="I1">
<BR>
<FONT SIZE=5><A ID="I2" NAME="I2"></A><B>16</B>
<BR></FONT></A></CENTER></H1>
<H2 ALIGN="CENTER">
<CENTER><A ID="I3" NAME="I3">
<FONT SIZE=5><B>Perl</B>
<BR></FONT></A></CENTER></H2>
<H5 ALIGN="CENTER">
<CENTER><A ID="I4" NAME="I4">
<FONT SIZE=3><B>By David Till</B>
<BR></FONT></A></CENTER></H5>
<H3 ALIGN="CENTER">
<CENTER><A ID="I5" NAME="I5">
<FONT SIZE=4><B>Overview</B>
<BR></FONT></A></CENTER></H3>
<P>The following sections tell you what Perl is and how you can get it, and provide a short example of a working Perl program.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I6" NAME="I6">
<FONT SIZE=3><B>What Is Perl?</B>
<BR></FONT></A></CENTER></H4>
<P>Perl is a simple yet useful programming language that provides the convenience of shell scripts and the power and flexibility of high-level programming languages. Perl programs are interpreted and executed directly, just as shell scripts are; however,
they also contain control structures and operators similar to those found in the C programming language. This gives you the ability to write useful programs in a very short time.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I7" NAME="I7">
<FONT SIZE=3><B>Where Can I Get Perl?</B>
<BR></FONT></A></CENTER></H4>
<P>Perl is freeware: it can be obtained by file transfer (ftp) from the Free Software Foundation at prep.ai.mit.edu (in the directory pub/gnu). Perl is also available from several other sites on the Internet, including any site that archives the newsgroup
comp.sources.unix.
<BR></P>
<P>The Free Software Foundation gives you the right to obtain Perl and its source, provided others have the right to obtain them from you. For more details on the Free Software Foundation licensing policy, refer to the file GETTING.GNU.SOFTWARE, also
available from the foundation.
<BR></P>
<H4 ALIGN="CENTER">
<CENTER><A ID="I8" NAME="I8">
<FONT SIZE=3><B>A Simple Sample Program</B>
<BR></FONT></A></CENTER></H4>
<P>To show easy it is to use Perl, here is a simple program that echoes (writes out) a line of input typed in at a terminal.
<BR></P>
<UL>
<LH><B>Listing 16.1. A Sample Perl Program.</B></LH></UL>
<PRE>#!/usr/bin/perl
$inputline = <STDIN>;
print ("$inputline");</PRE>
<P>To run this program, do the following:
<BR></P>
<OL>
<LI>Type in the program and save it in a file (say, foo).
<BR>
<BR></LI>
<LI>Tell the system that this file contains executable statements. To do this, enter the command chmod +x foo.
<BR>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -