PERL in 21 Days

Text-only Preview

Teach Yourself Perl 5 in 21 days
David Till
Table of Contents:
Introduction
q Who Should Read This Book?
q Special Features of This Book
q Programming Examples
q End-of-Day Q& A and Workshop
q Conventions Used in This Book
q What You'll Learn in 21 Days
Week 1 Week at a Glance
q Where You're Going
Day 1 Getting Started
q What Is Perl?
q How Do I Find Perl?
r Where Do I Get Perl?
r Other Places to Get Perl
q A Sample Perl Program
q Running a Perl Program
r If Something Goes Wrong
q The First Line of Your Perl Program: How Comments Work
r Comments
q Line 2: Statements, Tokens, and <STDIN>
r Statements and Tokens
r Tokens and White Space
r What the Tokens Do: Reading from Standard Input
q Line 3: Writing to Standard Output
r Function Invocations and Arguments
q Error Messages

q Interpretive Languages Versus Compiled Languages
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 2 Basic Operators and Control Flow
q Storing in Scalar Variables Assignment
r The Definition of a Scalar Variable
r Scalar Variable Syntax
r Assigning a Value to a Scalar Variable
q Performing Arithmetic
r Example of Miles-to-Kilometers Conversion
r The chop Library Function
q Expressions
r Assignments and Expressions
q Other Perl Operators
q Introduction to Conditional Statements
q The if Statement
r The Conditional Expression
r The Statement Block
r Testing for Equality Using ==
r Other Comparison Operators
q Two-Way Branching Using if and else
q Multi-Way Branching Using elsif
q Writing Loops Using the while Statement
q Nesting Conditional Statements
q Looping Using the until Statement
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 3 Understanding Scalar Values
q What Is a Scalar Value?
q Integer Scalar Values
r Integer Scalar Value Limitations

q Floating-Point Scalar Values
r Floating-Point Arithmetic and Round-Off Error
q Using Octal and Hexadecimal Notation
r Decimal Notation
r Octal Notation
r Hexadecimal Notation
r Why Bother?
q Character Strings
r Using Double-Quoted Strings
r Escape Sequences
r Single-Quoted Strings
q Interchangeability of Strings and Numeric Values
r Initial Values of Scalar Variables
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 4 More Operators
q Using the Arithmetic Operators
r Exponentiation
r The Remainder Operator
r Unary Negation
q Using Comparison Operators
r Integer-Comparison Operators
r String-Comparison Operators
r String Comparison Versus Integer Comparison
r Comparison and Floating-Point Numbers
q Using Logical Operators
r Evaluation Within Logical Operators
r Logical Operators as Subexpressions
q Using Bit-Manipulation Operators
r What Bits Are and How They Are Used
r The Bit-Manipulation Operators
q Using the Assignment Operators
r Assignment Operators as Subexpressions
q Using Autoincrement and Autodecrement
r The Autoincrement Operator Pre-Increment
r The Autoincrement Operator Post-Increment

r The Autodecrement Operator
r Using Autoincrement With Strings
q The String Concatenation and Repetition Operators
r The String-Concatenation Operator
r The String-Repetition Operator
r Concatenation and Assignment
q Other Perl Operators
r The Comma Operator
r The Conditional Operator
q The Order of Operations
r Precedence
r Associativity
r Forcing Precedence Using Parentheses
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 5 Lists and Array Variables
q Introducing Lists
q Scalar Variables and Lists
r Lists and String Substitution
q Storing Lists in Array Variables
q Accessing an Element of an Array Variable
r More Details on Array Element Names
q Using Lists and Arrays in Perl Programs
r Using Brackets and Substituting for Variables
q Using List Ranges
r Expressions and List Ranges
q More on Assignment and Array Variables
r Copying from One Array Variable to Another
r Using Array Variables in Lists
r Substituting for Array Variables in Strings
r Assigning to Scalar Variables from Array Variables
q Retrieving the Length of a List
q Using Array Slices
r Using List Ranges in Array-Slice Subscripts
r Using Variables in Array-Slice Subscripts
r Assigning to Array Slices

r Overlapping Array Slices
r Using the Array-Slice Notation as a Shorthand
q Reading an Array from the Standard Input File
q Array Library Functions
r Sorting a List or Array Variable
r Reversing a List or Array Variable
r Using chop on Array Variables
r Creating a Single String from a List
r Splitting a String into a List
r Other List-Manipulation Functions
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 6 Reading from and Writing to Files
q Opening a File
r The File Variable
r The Filename
r The File Mode
r Checking Whether the Open Succeeded
q Reading from a File
r File Variables and the Standard Input File
r Terminating a Program Using die
r Reading into Array Variables
q Writing to a File
r The Standard Output File Variable
r Merging Two Files into One
q Redirecting Standard Input and Standard Output
q The Standard Error File
q Closing a File
q Determining the Status of a File
r File-Test Operator Syntax
r Available File-Test Operators
r More on the -e Operator
r Testing for Read Permission-the -r Operator
r Checking for Other Permissions
r Checking for Empty Files
r Using File-Test Operators with File Variables

q Reading from a Sequence of Files
r Reading into an Array Variable
q Using Command-Line Arguments as Values
r ARGV and the <> Operator
q Opening Pipes
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 7 Pattern Matching
q Introduction
q The Match Operators
r Match-Operator Precedence
q Special Characters in Patterns
r The + Character
r The [] Special Characters
r The * and ? Special Characters
r Escape Sequences for Special Characters
r Matching Any Letter or Number
r Anchoring Patterns
r Variable Substitution in Patterns
r Excluding Alternatives
r Character-Range Escape Sequences
r Matching Any Character
r Matching a Specified Number of Occurrences
r Specifying Choices
r Reusing Portions of Patterns
r Pattern-Sequence Scalar Variables
r Special-Character Precedence
r Specifying a Different Pattern Delimiter
q Pattern-Matching Options
r Matching All Possible Patterns
r Ignoring Case
r Treating the String as Multiple Lines
r Evaluating a Pattern Only Once
r Treating the String as a Single Line
r Using White Space in Patterns
q The Substitution Operator

r Using Pattern-Sequence Variables in Substitutions
r Options for the Substitution Operator
r Evaluating a Pattern Only Once
r Treating the String as Single or Multiple Lines
r Using White Space in Patterns
r Specifying a Different Delimiter
q The Translation Operator
r Options for the Translation Operator
q Extended Pattern-Matching
r Parenthesizing Without Saving in Memory
r Embedding Pattern Options
r Positive and Negative Look-Ahead
r Pattern Comments
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Week 1 Week 1 in Review
Week 2 Week 2 at a Glance
q Where You're Going
Day 8 More Control Structures
q Using Single-Line Conditional Statements
r Problems with Single-Line Conditional Statements
q Looping Using the for Statement
r Using the Comma Operator in a for Statement
q Looping Through a List: The foreach Statement
r The foreach Local Variable
r Changing the Value of the Local Variable
r Using Returned Lists in the foreach Statement
q The do Statement
q Exiting a Loop Using the last Statement
q Using next to Start the Next Iteration of a Loop
q The redo Statement
q Using Labeled Blocks for Multilevel Jumps

r Using next and redo with Labels
q The continue Block
q The goto Statement
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 9 Using Subroutines
q What Is a Subroutine?
q Defining and Invoking a Subroutine
r Forward References to Subroutines
q Returning a Value from a Subroutine
r Return Values and Conditional Expressions
q The return Statement
q Using Local Variables in Subroutines
r Initializing Local Variables
q Passing Values to a Subroutine
r Passing a List to a Subroutine
q Calling Subroutines from Other Subroutines
q Recursive Subroutines
q Passing Arrays by Name Using Aliases
q Using the do Statement with Subroutines
q Specifying the Sort Order
q Predefined Subroutines
r Creating Startup Code Using BEGIN
r Creating Termination Code Using END
r Handling Non-Existent Subroutines Using AUTOLOAD
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 10 Associative Arrays
q Limitations of Array Variables
q Definition
q Referring to Associative Array Elements

q Adding Elements to an Associative Array
q Creating Associative Arrays
q Copying Associative Arrays from Array Variables
q Adding and Deleting Array Elements
q Listing Array Indexes and Values
q Looping Using an Associative Array
q Creating Data Structures Using Associative Arrays
r Linked Lists
r Structures
r Trees
r Databases
r Example: A Calculator Program
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 11 Formatting Your Output
q Defining a Print Format
q Displaying a Print Format
q Displaying Values in a Print Format
r Creating a General-Purpose Print Format
r Choosing a Value-Field Format
r Printing Value-Field Characters
r Using the Multiline Field Format
q Writing to Other Output Files
r Saving the Default File Variable
q Specifying a Page Header
r Changing the Header Print Format
q Setting the Page Length
r Using print with Pagination
q Formatting Long Character Strings
r Eliminating Blank Lines When Formatting
r Supplying an Indefinite Number of Lines
q Formatting Output Using printf
q Summary
q Q&A
q Workshop
r Quiz

r Exercises
Day 12 Working with the File System
q File Input and Output Functions
r Basic Input and Output Functions
r Skipping and Rereading Data
r System Read and Write Functions
r Reading Characters Using getc
r Reading a Binary File Using binmode
q Directory-Manipulation Functions
r The mkdir Function
r The chdir Function
r The opendir Function
r The closedir Function
r The readdir Function
r The telldir and seekdir Functions
r The rewinddir Function
r The rmdir Function
q File-Attribute Functions
r File-Relocation Functions
r Link and Symbolic Link Functions
r File-Permission Functions
r Miscellaneous Attribute Functions
q Using DBM Files
r The dbmopen Function
r The dbmclose Function
q Summary
q Q&A
q Workshop
r Quiz
r Exercises
Day 13 Process, String, and Mathematical Functions
q Process- and Program-Manipulation Functions
r Starting a Process
r Terminating a Program or Process
r Execution Control Functions
r Miscellaneous Control Functions
q Mathematical Functions

Document Outline

  • Perl Book
    • Table of Contents
    • Week 1 -- At a Glance
    • Day 1 -- Getting Started
    • Day 2 -- Basic Operators and Control Flow
    • Day 3 -- Understanding Scalar Values
    • Day 4 -- More Operators
    • Day 5 -- Lists and Array Variables
    • Day 6 -- Reading from and Writing to Files
    • Day 7 -- Pattern Matching
    • Week 1 -- In Review
    • Week 2 -- At a Glance
    • Day 8 -- More Control Structures
    • Day 9 -- Using Subroutines
    • Day 10 -- Associative Arrays
    • Day 11 -- Formatting Your Output
    • Day 12 -- Working with the File System
    • Day 13 -- Process, String, and Mathematical Functions
    • Day 14 -- Scalar-Conversion and List-Manipulation Functions
    • Week 2 -- In Review
    • Week 3 -- At a Glance
    • Day 15 -- System Functions
    • Day 16 -- Command-Line Options
    • Day 17 -- System Variables
    • Day 18 -- References in Perl 5
    • Day 19 -- Object-Oriented Programming in Perl
    • Day 20 -- Miscellaneous Features of Perl
    • Day 21 -- The Perl Debugger
    • Week 3 -- In Review
    • Appendices
      • Appendix A -- Answers
      • Appendix B -- ASCII Character Set