Teach Yourself Perl 5 in 21 Days
Total Page:16
File Type:pdf, Size:1020Kb
Teach Yourself Perl 5 in 21 days David Till Table of Contents: Introduction ● Who Should Read This Book? ● Special Features of This Book ● Programming Examples ● End-of-Day Q& A and Workshop ● Conventions Used in This Book ● What You'll Learn in 21 Days Week 1 Week at a Glance ● Where You're Going Day 1 Getting Started ● What Is Perl? ● How Do I Find Perl? ❍ Where Do I Get Perl? ❍ Other Places to Get Perl ● A Sample Perl Program ● Running a Perl Program ❍ If Something Goes Wrong ● The First Line of Your Perl Program: How Comments Work ❍ Comments ● Line 2: Statements, Tokens, and <STDIN> ❍ Statements and Tokens ❍ Tokens and White Space ❍ What the Tokens Do: Reading from Standard Input ● Line 3: Writing to Standard Output ❍ Function Invocations and Arguments ● Error Messages ● Interpretive Languages Versus Compiled Languages ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 2 Basic Operators and Control Flow ● Storing in Scalar Variables Assignment ❍ The Definition of a Scalar Variable ❍ Scalar Variable Syntax ❍ Assigning a Value to a Scalar Variable ● Performing Arithmetic ❍ Example of Miles-to-Kilometers Conversion ❍ The chop Library Function ● Expressions ❍ Assignments and Expressions ● Other Perl Operators ● Introduction to Conditional Statements ● The if Statement ❍ The Conditional Expression ❍ The Statement Block ❍ Testing for Equality Using == ❍ Other Comparison Operators ● Two-Way Branching Using if and else ● Multi-Way Branching Using elsif ● Writing Loops Using the while Statement ● Nesting Conditional Statements ● Looping Using the until Statement ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 3 Understanding Scalar Values ● What Is a Scalar Value? ● Integer Scalar Values ❍ Integer Scalar Value Limitations ● Floating-Point Scalar Values ❍ Floating-Point Arithmetic and Round-Off Error ● Using Octal and Hexadecimal Notation ❍ Decimal Notation ❍ Octal Notation ❍ Hexadecimal Notation ❍ Why Bother? ● Character Strings ❍ Using Double-Quoted Strings ❍ Escape Sequences ❍ Single-Quoted Strings ● Interchangeability of Strings and Numeric Values ❍ Initial Values of Scalar Variables ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 4 More Operators ● Using the Arithmetic Operators ❍ Exponentiation ❍ The Remainder Operator ❍ Unary Negation ● Using Comparison Operators ❍ Integer-Comparison Operators ❍ String-Comparison Operators ❍ String Comparison Versus Integer Comparison ❍ Comparison and Floating-Point Numbers ● Using Logical Operators ❍ Evaluation Within Logical Operators ❍ Logical Operators as Subexpressions ● Using Bit-Manipulation Operators ❍ What Bits Are and How They Are Used ❍ The Bit-Manipulation Operators ● Using the Assignment Operators ❍ Assignment Operators as Subexpressions ● Using Autoincrement and Autodecrement ❍ The Autoincrement Operator Pre-Increment ❍ The Autoincrement Operator Post-Increment ❍ The Autodecrement Operator ❍ Using Autoincrement With Strings ● The String Concatenation and Repetition Operators ❍ The String-Concatenation Operator ❍ The String-Repetition Operator ❍ Concatenation and Assignment ● Other Perl Operators ❍ The Comma Operator ❍ The Conditional Operator ● The Order of Operations ❍ Precedence ❍ Associativity ❍ Forcing Precedence Using Parentheses ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 5 Lists and Array Variables ● Introducing Lists ● Scalar Variables and Lists ❍ Lists and String Substitution ● Storing Lists in Array Variables ● Accessing an Element of an Array Variable ❍ More Details on Array Element Names ● Using Lists and Arrays in Perl Programs ❍ Using Brackets and Substituting for Variables ● Using List Ranges ❍ Expressions and List Ranges ● More on Assignment and Array Variables ❍ Copying from One Array Variable to Another ❍ Using Array Variables in Lists ❍ Substituting for Array Variables in Strings ❍ Assigning to Scalar Variables from Array Variables ● Retrieving the Length of a List ● Using Array Slices ❍ Using List Ranges in Array-Slice Subscripts ❍ Using Variables in Array-Slice Subscripts ❍ Assigning to Array Slices ❍ Overlapping Array Slices ❍ Using the Array-Slice Notation as a Shorthand ● Reading an Array from the Standard Input File ● Array Library Functions ❍ Sorting a List or Array Variable ❍ Reversing a List or Array Variable ❍ Using chop on Array Variables ❍ Creating a Single String from a List ❍ Splitting a String into a List ❍ Other List-Manipulation Functions ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 6 Reading from and Writing to Files ● Opening a File ❍ The File Variable ❍ The Filename ❍ The File Mode ❍ Checking Whether the Open Succeeded ● Reading from a File ❍ File Variables and the Standard Input File ❍ Terminating a Program Using die ❍ Reading into Array Variables ● Writing to a File ❍ The Standard Output File Variable ❍ Merging Two Files into One ● Redirecting Standard Input and Standard Output ● The Standard Error File ● Closing a File ● Determining the Status of a File ❍ File-Test Operator Syntax ❍ Available File-Test Operators ❍ More on the -e Operator ❍ Testing for Read Permission-the -r Operator ❍ Checking for Other Permissions ❍ Checking for Empty Files ❍ Using File-Test Operators with File Variables ● Reading from a Sequence of Files ❍ Reading into an Array Variable ● Using Command-Line Arguments as Values ❍ ARGV and the <> Operator ● Opening Pipes ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 7 Pattern Matching ● Introduction ● The Match Operators ❍ Match-Operator Precedence ● Special Characters in Patterns ❍ The + Character ❍ The [] Special Characters ❍ The * and ? Special Characters ❍ Escape Sequences for Special Characters ❍ Matching Any Letter or Number ❍ Anchoring Patterns ❍ Variable Substitution in Patterns ❍ Excluding Alternatives ❍ Character-Range Escape Sequences ❍ Matching Any Character ❍ Matching a Specified Number of Occurrences ❍ Specifying Choices ❍ Reusing Portions of Patterns ❍ Pattern-Sequence Scalar Variables ❍ Special-Character Precedence ❍ Specifying a Different Pattern Delimiter ● Pattern-Matching Options ❍ Matching All Possible Patterns ❍ Ignoring Case ❍ Treating the String as Multiple Lines ❍ Evaluating a Pattern Only Once ❍ Treating the String as a Single Line ❍ Using White Space in Patterns ● The Substitution Operator ❍ Using Pattern-Sequence Variables in Substitutions ❍ Options for the Substitution Operator ❍ Evaluating a Pattern Only Once ❍ Treating the String as Single or Multiple Lines ❍ Using White Space in Patterns ❍ Specifying a Different Delimiter ● The Translation Operator ❍ Options for the Translation Operator ● Extended Pattern-Matching ❍ Parenthesizing Without Saving in Memory ❍ Embedding Pattern Options ❍ Positive and Negative Look-Ahead ❍ Pattern Comments ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Week 1 Week 1 in Review Week 2 Week 2 at a Glance ● Where You're Going Day 8 More Control Structures ● Using Single-Line Conditional Statements ❍ Problems with Single-Line Conditional Statements ● Looping Using the for Statement ❍ Using the Comma Operator in a for Statement ● Looping Through a List: The foreach Statement ❍ The foreach Local Variable ❍ Changing the Value of the Local Variable ❍ Using Returned Lists in the foreach Statement ● The do Statement ● Exiting a Loop Using the last Statement ● Using next to Start the Next Iteration of a Loop ● The redo Statement ● Using Labeled Blocks for Multilevel Jumps ❍ Using next and redo with Labels ● The continue Block ● The goto Statement ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 9 Using Subroutines ● What Is a Subroutine? ● Defining and Invoking a Subroutine ❍ Forward References to Subroutines ● Returning a Value from a Subroutine ❍ Return Values and Conditional Expressions ● The return Statement ● Using Local Variables in Subroutines ❍ Initializing Local Variables ● Passing Values to a Subroutine ❍ Passing a List to a Subroutine ● Calling Subroutines from Other Subroutines ● Recursive Subroutines ● Passing Arrays by Name Using Aliases ● Using the do Statement with Subroutines ● Specifying the Sort Order ● Predefined Subroutines ❍ Creating Startup Code Using BEGIN ❍ Creating Termination Code Using END ❍ Handling Non-Existent Subroutines Using AUTOLOAD ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 10 Associative Arrays ● Limitations of Array Variables ● Definition ● Referring to Associative Array Elements ● Adding Elements to an Associative Array ● Creating Associative Arrays ● Copying Associative Arrays from Array Variables ● Adding and Deleting Array Elements ● Listing Array Indexes and Values ● Looping Using an Associative Array ● Creating Data Structures Using Associative Arrays ❍ Linked Lists ❍ Structures ❍ Trees ❍ Databases ❍ Example: A Calculator Program ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 11 Formatting Your Output ● Defining a Print Format ● Displaying a Print Format ● Displaying Values in a Print Format ❍ Creating a General-Purpose Print Format ❍ Choosing a Value-Field Format ❍ Printing Value-Field Characters ❍ Using the Multiline Field Format ● Writing to Other Output Files ❍ Saving the Default File Variable ● Specifying a Page Header ❍ Changing the Header Print Format ● Setting the Page Length ❍ Using print with Pagination ● Formatting Long Character Strings ❍ Eliminating Blank Lines When Formatting ❍ Supplying an Indefinite Number of Lines ● Formatting Output Using printf ● Summary ● Q&A ● Workshop ❍ Quiz ❍ Exercises Day 12 Working with the File System ● File Input and Output Functions ❍ Basic Input and Output Functions ❍ Skipping and Rereading Data ❍ System Read and Write Functions ❍ Reading Characters