Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 665 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
665
Dung lượng
4,2 MB
Nội dung
Perl 5 by Example by David Medinets C O N T E N T S Chapter 1 Getting Your Feet Wet Origins● Similar to C?● Cost and Licensing● Do You Have Perl Installed?● Getting and Installing Perl● Your First Perl Program Creating the Program❍ Invocation❍ ● Comments in Your Program● Summary● Review Questions● Review Exercises● Chapter 2 Numeric and String Literals Numeric Literals Example: Numbers❍ ● String Literals Example: Single-Quoted Strings❍ Example: Double-Quoted Strings❍ ● Example: Back-Quoted Strings❍ Array Literals Example: Printing an Array❍ Example: Nesting Arrays❍ Example: Using a Range of Values❍ ● Summary● Review Questions● Review Exercises● Chapter 3 Variables Scalar Variables Example: Assigning Values to Scalar Variables❍ Changing Values in Scalar Variables❍ ● Array Variables Example: Assigning Values to Array Variables❍ Example: Using Array Elements❍ Example: Using Negative Subscripts❍ Example: Determining the Number of Elements in an Array❍ Example: How to Grab a Slice (or Part) of an Array❍ ● Associative Array Variables Example: Assigning Values to Associative Array Variables❍ ● Double-Quoted Strings Revisited Example: Variable Interpolation❍ Example: Using the $" Special Variable❍ ● Summary● Review Questions● Review Exercises● Chapter 4 Operators Operator Types● The Binary Arithmetic Operators Example: The Exponentiation Operator❍ Example: The Modulus Operator❍ ● The Unary Arithmetic Operators Example: The Pre-iNCrement Operator❍ Example: The Pre-decrement Operator❍ Example: The Post-iNCrement Operator❍ ● The Logical Operators Example: The "AND" Operator (&&)❍ Example: The "OR" Operator (||)❍ ● Example: The "NOT" Operator (!)● The Bitwise Operators Example: Using the &, |, and ^ Operators❍ Example: Using the >> and << Operators❍ ● The Numeric Relational Operators Example: Using the <=> Operator❍ ● The String Relational Operators Example: Using the cmp Operator❍ ● The Ternary Operator Example: Using the Ternary Operator to Assign Values❍ ● The Range Operator ( ) Example: Using the Range Operator❍ ● The String Operators (. and x) Example: Using the CoNCatenation Operator❍ Example: Using the Repetition Operator❍ ● The Assignment Operators Example: Assignment Using Array Slices❍ Example: Assigning an Array to Scalar Variables❍ ● Order of PrecedeNCe Example: Order of PrecedeNCe❍ ● Summary● Review Questions● Review Exercises● Chapter 5 FuNCtions Example: Using the Parameter Array (@_)❍ Example: Passing Parameters by RefereNCe❍ Example: Scope of Variables❍ Example: Using a List as a FuNCtion Parameter❍ Example: Nesting FuNCtion Calls❍ Example: Using a Private FuNCtion❍ String FuNCtions Example: Changing a String's Value❍ Example: Searching a String❍ ● Array FuNCtions Example: Printing an Associative Array❍ Example: Checking the ExisteNCe of an Element❍ ● Summary● Review Questions● Review Exercises● Chapter 6 Statements Understanding Expressions● Statement Blocks● Statement Blocks and Local Variables● Statement Types Example: Using the if Modifier❍ Example: Using the unless Modifier❍ Example: Using the until Modifier❍ Example: Using the while Modifier❍ ● Summary● Review Questions● Review Exercises● Chapter 7 Control Statements Decision Statements Example: The if Statement❍ ● Loop Statements Example: While Loops❍ Example: Until Loops❍ Example: For Loops❍ Example: Foreach Loops❍ ● Jump Keywords Example: The last Keyword❍ Example: The next Keyword❍ Example: The redo Keyword❍ Example: The goto Keyword❍ ● Summary● Review Questions● Review Exercises● Chapter 8 RefereNCes RefereNCe Types Example: Passing Parameters to FuNCtions❍ Example: The ref() FuNCtion❍ Example: Creating a Data Record❍ Example: Interpolating FuNCtions Inside Double-Quoted Strings❍ ● Summary● Review Questions● Review Exercises● Chapter 9 Using Files Some Files Are Standard Example: Using STDIN❍ Example: Using Redirection to Change STDIN and STDOUT❍ Example: Using the Diamond Operator (<>)❍ ● File Test Operators Example: Using File Tests❍ ● File FuNCtions Example: Opening Files❍ Example: Binary Files❍ Example: Getting File Statistics❍ Example: Using the Directory FuNCtions❍ Example: Printing Revisited❍ ● Globbing Example: Assigning a Glob to an Array❍ ● Using Data Structures with Files Example: Splitting a Record into Fields❍ ● Summary● Review Questions● Review Exercises● Chapter 10 Regular Expressions Pattern Delimiters● The Matching Operator (m//) The Matching Options❍ ● The Substitution Operator (s///) The Substitution Options❍ ● The Translation Operator (tr///) The Translation Options❍ ● The Binding Operators (=~ and !~)● How to Create Patterns Example: Character Classes❍ Example: Quantifiers❍ Example: Pattern Memory❍ Example: Pattern PrecedeNCe❍ Example: Extension Syntax❍ ● Pattern Examples Example: Using the Match Operator❍ ● Example: Using the Substitution Operator❍ Example: Using the Translation Operator❍ Example: Using the Split() FuNCtion❍ Summary● Review Questions● Review Exercises● Chapter 11 Creating Reports What's a Format Statement? Example: Using Field Lines❍ Example: Report Headings❍ Example: Using FuNCtions in the Value Line❍ Example: Changing Formats❍ Example: Using Long Pieces of Text in Reports❍ Example: Writing to a File Instead of STDOUT❍ ● Summary● Review Questions● Review Exercises● Chapter 12 Using Special Variables What Are the Special Variables? Example: Using the DATA File Handle❍ ● Summary● Review Questions● Review Exercises● Chapter 13 Handling Errors and Signals Checking for Errors● Example: Using the errno Variable Example: Using the or Logical Operator❍ Example: Using the die() FuNCtion❍ Example: Using the warn() FuNCtion❍ ● Trapping Fatal Errors● Example: Using the eval() FuNCtion❍ What Is a Signal? Example: How to Handle a Signal❍ ● Summary● Review Questions● Review Exercises● Chapter 14 What Are Objects? Learning about Classes● Abstract Thinking● Overriding Methods with Polymorphism● Keeping Code and Data Together with ENCapsulation● How Perl Handles Objects Example: Bless the Hash and Pass the RefereNCe❍ Example: Initializing Properties❍ Example: Using Named Parameters in Constructors❍ Example: InheritaNCe, Perl Style❍ Example: Polymorphism❍ Example: How One Class Can Contain Another❍ ● Static Versus Regular Methods and Variables● Summary● Review Questions● Review Exercises● Chapter 15 Perl Modules Module Constructors and Destructors The BEGIN Block❍ The END Block❍ ● Symbol Tables● The require Compiler Directive● The use Compiler Directive● What's a Pragma?● The strict Pragma● The Standard Modules● strict, my() and Modules● Module Examples Example: The Carp Module❍ Example: The English Module❍ Example: The Env Module❍ ● Summary● Review Questions● Review Exercises● Chapter 16 Debugging Perl Syntax Errors● Common Syntax Errors● Logic Errors Using the -w Command-Line Option❍ Being Strict with Your Variables❍ Stepping Through Your Script❍ Examples: Displaying Information❍ Examples: Using the n Command❍ Examples: Using Breakpoints❍ Examples: Creating Command Aliases❍ Examples: Using the Debugger as an Interactive Interpreter❍ ● Summary● Review Questions● Review Exercises● Chapter 17 Using Command-Line Options How Are the Options Specified?● What Are the Options? Example: Using the -0 Option❍ Example: Using the -n and -p Options❍ Example: Using the -i Option❍ Example: Using the -s Option❍ ● Summary● Review Questions● Review Exercises● Chapter 18 Using Internet Protocols Sockets The Server Side of a Conversation❍ The Client Side of a Conversation❍ ● Using the Time Service● Sending Mail (SMTP) The MAIL Command❍ The RCPT Command❍ The DATA Command❍ Reporting Undeliverable Mail❍ Using Perl to Send Mail❍ ● Receiving Mail (POP)● Checking for Upness (Echo)● Transferring Files (FTP)● Reading the News (NNTP)● The World Wide Web (HTTP)● Summary● Review Questions● Review Exercises● Chapter 19 What Is CGI? Why Use Perl for CGI?● CGI Apps versus Java Applets● Should You Use CGI Modules?● How Does CGI Work?● Calling Your CGI Program● Your First CGI Program● Why Are File Permissions Important in UNIX?● HTTP Headers● [...]... test.pl file while reading the previous section, you can run it like this: perl test.pl This example assumes that perl is in the execution path; if not, you will need to supply the full path to perl, too For example, on UNIX the command might be: /usr/local/bin /perl test.pl Whereas on Windows NT, you might need to use: c: \perl5 \bin \perl test.pl UNIX systems have another way to invoke a program However,... learn -by- doing priNCiple because I believe that simply reading about a subject makes it harder to learn After all, you don't read about putting together a jigsaw puzzle; you put the puzzle together yourself! Programming is the same way You must actually run some programs in order to really understand the coNCepts Perl 5 By Example will teach you how to use the Perl programming language by showing examples... is perl, version 5. 001 Unofficial patchlevel 1m Copyright 1987-1994, Larry Wall Win32 port Copyright 19 95 Microsoft Corporation All rights reserved Developed by hip communications iNC., http://info.hip.com/info/ Perl for Win32 Build 107 Built Apr 16 1996@14:47:22 Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5. 0... getting and installing Perl For UNIX and OS/2-The Perl Home Page contains a software link (http://www .perl. com /perl/ info/software.html) that will enable you to download the latest Perl source code The page also explains why Perl binaries are not available Hopefully, your system will already have Perl installed If not, try to get your system administrator to install it For Windows 95/ Windows NT-The home... Review Exercises Chapter 21 Using Perl with Web Servers q Server Log Files r Example: Reading a Log File r r Example: Looking at the Status Code r Example: Converting the Report to a Web Page r Existing Log File Analyzing Programs r q Example: Listing Access by Document Creating Your Own CGI Log File Communicating with Users r Example: Generating a What's New Page r Example: Getting User Feedback q... the answers can be a big confideNCe builder Who Should Use This Book? Perl 5 By Example should be read by anyone seeking to learn Perl If you don't know any other programming languages, Chapters 2 through 7 will give you a solid introduction to the basics If you already know another language, then skip Chapters 2 through 7 to see how Perl differs from other languages and start with Chapter 8, "RefereNCes."... Resources," connect to the #perl IRC channel, and ask for help Don't be shy! Your First Perl Program Your first Perl program will show how to display a line of text on your monitor First, you create a text file to hold the Perl program Then you run or execute the Perl program file Creating the Program A Perl program consists of an ordinary text file containing a series of Perl statements Statements... (NAME) r gethostbyaddr (ADDRESS, AF_INIT) r gethostbyname (NAME, [PROTOCOL]) r gethostent ( ) r getlogin ( ) r getnetbyaddr (ADDRESS, ADDR_TYPE) r getnetbyname (NAME) r getnetent ( ) r getpeername (SOCKET) r getpgrp (PID) r getppid ( ) r getpriority (WHICH, WHO) r getprotobyname (NAME) r getprotobynumber (NUMBER) r getprotoent ( ) r getpwent ( ) r getpwnam (NAME) r getpwuid (UID) r getservbyname (NAME,... Similar to C? q Cost and Licensing q Do You Have Perl Installed? q Getting and Installing Perl q Your First Perl Program r Creating the Program r Invocation q Comments in Your Program q Summary q Review Questions q Review Exercises You are about to embark on a journey through the world of Perl programming You'll find that the trip has been `made easier by many examples liberally sprinkled along the trail... of Perl Then, I realized that when I was first learning computer languages, I didn't really care about that stuff I just wanted to know about the language and what I could do with it With that in mind, the next section on Perl' s origin is very short After all, you can read all the background information you'd like using a web browser by starting at http://www .perl. com-the Perl Home Page Origins Perl . Perl 5 by Example by David Medinets C O N T E N T S Chapter 1 Getting Your Feet Wet Origins● Similar to C?● Cost and Licensing● Do You Have Perl Installed?● Getting and Installing Perl . Chapter 5 FuNCtions Example: Using the Parameter Array (@_)❍ Example: Passing Parameters by RefereNCe❍ Example: Scope of Variables❍ Example: Using a List as a FuNCtion Parameter❍ Example: . Statements Example: The if Statement❍ ● Loop Statements Example: While Loops❍ Example: Until Loops❍ Example: For Loops❍ Example: Foreach Loops❍ ● Jump Keywords Example: The last Keyword❍ Example: