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
chopLibrary Function
❍
●
Expressions
Assignments and Expressions
❍
●
Other Perl Operators
●
Introduction to Conditional Statements
●
The
ifStatement
The Conditional Expression
❍
The Statement Block
❍
Testing for Equality Using
==❍
Other Comparison Operators
❍
●
Two-Way Branching Using
ifand
else●
Multi-Way Branching Using
elsif●
Writing Loops Using the
whileStatement
●
Nesting Conditional Statements
●
Looping Using the
untilStatement
●
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
chopon 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
-eOperator
❍
Testing for Read Permission-the
-rOperator
❍
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
ARGVand 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
forStatement
Using the Comma Operator in a
forStatement
❍
●
Looping Through a List: The
foreachStatement The
foreachLocal Variable
❍
Changing the Value of the Local Variable
❍
Using Returned Lists in the
foreachStatement
❍
●
The
doStatement
●
Exiting a Loop Using the
lastStatement
●
Using
nextto Start the Next Iteration of a Loop
●
The
redoStatement
●
Using Labeled Blocks for Multilevel Jumps Using
nextand
redowith Labels
❍
●
The
continueBlock
●
The
gotoStatement
●
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
returnStatement
●
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
doStatement 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 Using
getc❍
Reading a Binary File Using
binmode❍
●
Directory-Manipulation Functions The
mkdirFunction
❍
The
chdirFunction
❍
The
opendirFunction
❍
The
closedirFunction
❍
The
readdirFunction
❍
The
telldirand
seekdirFunctions
❍
The rewinddir Function
❍
The
rmdirFunction
❍
●
File-Attribute Functions
File-Relocation Functions
❍
Link and Symbolic Link Functions
❍
File-Permission Functions
❍
Miscellaneous Attribute Functions
❍
●
Using DBM Files
The
dbmopenFunction
❍
The
dbmcloseFunction
❍
●
Summary
●
Q&A
●
Workshop Quiz
❍
Exercises
❍
●
Day 13 Process, String, and Mathematical Functions
Process- and Program-Manipulation Functions Starting a Process
❍
Terminating a Program or Process
❍
Execution Control Functions
❍
Miscellaneous Control Functions
❍
●
Mathematical Functions
The
sinand
cosFunctions
❍
The
atan2Function
❍
The
sqrtFunction
❍
The
expFunction
❍
The
logFunction
❍
The
absFunction
❍
The
randand
srandFunctions
❍
●
String-Manipulation Functions The
indexFunction
❍
The
rindexFunction
❍
The length Function
❍
Retrieving String Length Using
tr❍
The
posFunction
❍
The
substrFunction
❍
The
studyFunction
❍
Case Conversion Functions
❍
The
quotemetaFunction
❍
The
joinFunction
❍
The
sprintfFunction
❍
●
Summary
●
Q&A
●
Workshop Quiz
❍
Exercises
❍
●
Day 14 Scalar-Conversion and List-Manipulation Functions
The
chopFunction
●
The
chompFunction
●
The
cryptFunction
●
The
hexFunction
●
The
intFunction
●
The
octFunction
The
octFunction and Hexadecimal Integers
❍
●
The ord and chr Functions
●
The
scalarFunction
●
The
packFunction
The
packFunction and C Data Types
❍
●
The
unpackFunction Unpacking Strings
❍
Skipping Characters When Unpacking
❍
The
unpackFunction and
uuencode❍
●
The
vecFunction
●
The
definedFunction
●
The
undefFunction
●
Array and List Functions The
grepFunction
❍
The
spliceFunction
❍
The
shiftFunction
❍
The
unshiftFunction
❍
The
pushFunction
❍
The
popFunction
❍
Creating Stacks and Queues
❍
The
splitFunction
❍
The sort and reverse Functions
❍
The
mapFunction
❍
The
wantarrayFunction
❍
●
Associative Array Functions The
keysFunction
❍
The
valuesFunction
❍
●
The
eachFunction
❍
The
deleteFunction
❍
The
existsFunction
❍
Summary
●
Q&A
●
Workshop Quiz
❍
Exercises
❍
●
Week 2 Week 2 in Review Week 3 Week 3 at a Glance
Where You're Going
●
Day 15 System Functions
System Library Emulation Functions The
getgrentFunction
❍
The
setgrentand
endgrentFunctions
❍
The
getgrnamFunction
❍
The
getgridFunction
❍
The
getnetentFunction
❍
The
getnetbyaddrFunction
❍
The
getnetbynameFunction
❍
The
setnetentand
endnetentFunctions
❍
The
gethostbyaddrFunction
❍
The
gethostbynameFunction
❍
The gethostent, sethostent, and endhostent Functions
❍
The
getloginFunction
❍
The
getpgrpand
setpgrpFunctions
❍
The
getppidFunction
❍
The
getpwnamFunction
❍
The
getpwuidFunction
❍
The
getpwentFunction
❍
The
setpwentand
endpwentFunctions
❍
The
getpriorityand
setpriorityFunctions
❍
●
The
getprotoentFunction
❍
The
getprotobynameand
getprotobynumberFunctions
❍
The setprotoent and endprotoent Functions
❍
The
getserventFunction
❍
The
getservbynameand
getservbyportFunctions
❍
The
setserventand
endserventFunctions
❍
The
chrootFunction
❍
The
ioctlFunction
❍
The
alarmFunction
❍
Calling the System
selectFunction
❍
The
dumpFunction
❍
Socket-Manipulation Functions The
socketFunction
❍
The
bindFunction
❍
The
listenFunction
❍
The
acceptFunction
❍
The
connectFunction
❍
The
shutdownFunction
❍
The socketpair Function
❍
The
getsockoptand
setsockoptFunctions
❍
The
getsocknameand
getpeernameFunctions
❍
●
The UNIX System V IPC Functions
IPC Functions and the
requireStatement
❍
The
msggetFunction
❍
The
msgsndFunction
❍
The
msgrcvFunction
❍
The
msgctlFunction
❍
The
shmgetFunction
❍
The
shmwriteFunction
❍
The
shmreadFunction
❍
The
shmctlFunction
❍
The
semgetFunction
❍
The
semopFunction
❍
The
semctlFunction
❍
●
Summary
●
Q&A
●
Workshop Quiz
❍
Exercises
❍
●
Day 16 Command-Line Options
Specifying Options
Specifying Options on the Command Line
❍
Specifying an Option in the Program
❍
●
The
-vOption: Printing the Perl Version Number
●
The
-cOption: Checking Your Syntax
●
The
-wOption: Printing Warnings Checking for Possible Typos
❍
Checking for Redefined Subroutines
❍
Checking for Incorrect Comparison Operators
❍
●
The
-eOption: Executing a Single-Line Program
●
The
-sOption: Supplying Your Own Command-Line Options The
-sOption and Other Command-Line Arguments
❍
●
The
-POption: Using the C Preprocessor The C Preprocessor: A Quick Overview
❍
●
The
-IOption: Searching for C Include Files
●
The
-nOption: Operating on Multiple Files
●
The -p Option: Operating on Files and Printing
●
The
-iOption: Editing Files
Backing Up Input Files Using the
-iOption
❍
●
The
-aOption: Splitting Lines
●
The
-FOption: Specifying the Split Pattern
●
The
-0Option: Specifying Input End-of-Line
●
The
-lOption: Specifying Output End-of-Line
●
The
-xOption: Extracting a Program from a Message
●
Miscellaneous Options The
-uOption
❍
The
-UOption
❍
The
-SOption
❍
The
-DOption
❍
The
-TOption: Writing Secure Programs
❍
●
The
-dOption: Using the Perl Debugger
●
Summary
●
Q&A
●
Workshop Quiz
❍
Exercises
❍
●
Day 17 System Variables
Global Scalar Variables
The Default Scalar Variable:
$_❍
The Program Name:
$0❍
The User ID:
$<and
$>❍
The Group ID:
$(and
$)❍
The Version Number:
$]❍
The Input Line Separator:
$/❍
The Output Line Separator:
$❍
The Output Field Separator: $,
❍
The Array Element Separator:
$"❍
The Number Output Format:
$#❍
The
evalError Message:
$@❍
The System Error Code:
$?❍
The System Error Message:
$!❍
The Current Line Number:
$.❍
Multiline Matching:
$*❍
The First Array Subscript:
$[❍
Multidimensional Associative Arrays and the
$;Variable
❍
The Word-Break Specifier:
$:❍
The Perl Process ID:
$$❍
The Current Filename:
$ARGV❍
The Write Accumulator:
$^A❍
The Internal Debugging Value:
$^D❍
The System File Flag:
$^F❍
Controlling File Editing Using
$^I❍
The Format Form-Feed Character:
$^L❍
Controlling Debugging:
$^P❍
●
The Program Start Time:
$^T❍
Suppressing Warning Messages:
$^W❍
The
$^XVariable
❍
Pattern System Variables
Retrieving Matched Subpatterns
❍
Retrieving the Entire Pattern:
$&❍
Retrieving the Unmatched Text: the $` and $' Variables
❍
The
$+Variable
❍
●
File System Variables
The Default Print Format:
$~❍
Specifying Page Length:
$=❍
Lines Remaining on the Page:
$-❍
The Page Header Print Format:
$^❍
Buffering Output:
$|❍
The Current Page Number:
$%❍
●
Array System Variables The
@_Variable
❍
The
@ARGVVariable
❍
The
@FVariable
❍
The
@INCVariable
❍
The
%INCVariable
❍
The
%ENVVariable
❍
The
%SIGVariable
❍
●
Built-In File Variables
STDIN
,
STDOUT, and
STDERR❍
ARGV
❍
DATA
❍
The Underscore File Variable
❍
●
Specifying System Variable Names as Words
●
Summary
●
Q&A
●
Workshop Quiz
❍
Exercises
❍
●
Day 18 References in Perl 5
Introduction to References
●
Using References
●
Using the Backslash Operator
●
References and Arrays
●
Multidimensional Arrays
●
References to Subroutines
Using Subroutine Templates
❍
●
Using Subroutines to Work with Multiple Arrays Pass By Value or By Reference?
❍
●
References to File Handles
What Does the
*variableOperator Do?
❍
●
Using Symbolic References… Again
Declaring Variables with Curly Braces
❍
●
More on Hard Versus Symbolic References
●
For More Information
●
Summary
●
Q&A
●
Workshop Quiz
❍
●
Exercises
●
Day 19 Object-Oriented Programming in Perl
An Introduction to Modules The Three Important Rules
❍
●
Classes in Perl
●
Creating a Class
●
Blessing a Constructor Instance Variables
❍
●
Methods
●
Exporting Methods
●
Invoking Methods
●
Overrides
●
Destructors
●
Inheritance
●
Overriding Methods
●
A Few Comments About Classes and Objects in Perl
●
Summary
●
Q&A
●
Workshop Quiz
❍
Exercises
❍
●
Day 20 Miscellaneous Features of Perl
The
requireFunction
The
requireFunction and Subroutine Libraries
❍
Using
requireto Specify a Perl Version
❍
●
The
$#arrayVariables
Controlling Array Length Using
$#array❍
●
Alternative String Delimiters Defining Strings Using
<<❍
●
Special Internal Values
●
Using Back Quotes to Invoke System Commands
●
Pattern Matching Using
??and the
resetFunction Using
resetwith Variables
❍
●
Other Features of the
<>Operator
Scalar Variable Substitution and
<>❍
Creating a List of Filenames
❍
●
Global Indirect References and Aliases
●
Packages
Defining a Package
❍
Switching Between Packages
❍
The
mainPackage
❍
Referring to One Package from Another
❍
Specifying No Current Package
❍
Packages and Subroutines
❍
Defining Private Data Using Packages
❍
Packages and System Variables
❍
Accessing Symbol Tables
❍
●
Modules
Creating a Module
❍
Importing Modules Into Your Program
❍
Using Predefined Modules
❍
●
Using Perl in C Programs
●
Perl and CGI Scripts
●
Translators and Other Supplied Code
●
Summary
●
Q&A
●
Workshop Quiz
❍
Exercises
❍
●
Day 21 The Perl Debugger
Entering and Exiting the Perl Debugger Entering the Debugger
❍
Exiting the Debugger
❍
●
Listing Your Program The
lcommand
❍
The
-Command
❍
The
wCommand
❍
The
//and
??Commands
❍
The
SCommand
❍
●
Stepping Through Programs The
sCommand
❍
The
nCommand
❍
The
fcommand
❍
The Carriage-Return Command
❍
The
rCommand
❍
●
Displaying Variable Values The
XCommand
❍
The
VCommand
❍
●
Breakpoints
The
bCommand
❍
The
cCommand
❍
●
The
LCommand and Breakpoints
❍
The
dand
DCommands
❍
Tracing Program Execution
●
Line Actions
The
aCommand
❍
The
ACommand
❍
The < and > Commands
❍
Displaying Line Actions Using the
LCommand
❍
●
Other Debugging Commands
Executing Other Perl Statements
❍
The
HCommand: Listing Preceding Commands
❍
The
!Command: Executing Previous Commands
❍
The
TCommand: Stack Tracing
❍
The
pCommand: Printing an Expression
❍
The
=Command: Defining Aliases
❍
Predefining Aliases
❍
The
hCommand: Debugger Help
❍
●
Summary
●
Q&A
●
Workshop Quiz
❍
●
Week 3 Week 3 in Review Appendix A Answers
Answers for Day 1, "Getting Started"
Quiz
❍
Exercises
❍
●
Answers for Day 2, "Basic Operators and Control Flow"
Quiz
❍
Exercises
❍
●
Answers for Day 3, "Understanding Scalar Values"
Quiz
❍
Exercises
❍
●
Answers for Day 4, "More Operators"
●
Quiz
❍
Exercises
❍
Answers for Day 5, "Lists and Array Variables"
Quiz
❍
Exercises
❍
●
Answers for Day 6, "Reading from and Writing to Files"
Quiz
❍
Exercises
❍
●
Answers for Day 7, "Pattern Matching"
Quiz
❍
Exercises
❍
●
Answers for Day 8, "More Control Structures"
Quiz
❍
Exercises
❍
●
Answers for Day 9, "Using Subroutines"
Quiz
❍
Exercises
❍
●
Answers for Day 10, "Associative Arrays"
Quiz
❍
Exercises
❍
●
Answers for Day 11, "Formatting Your Output"
Quiz
❍
Exercises
❍
●
Answers for Day 12, "Working with the File System"
Quiz
❍
Exercises
❍
●
Answers for Day 13, "Process, String, and Mathematical Functions"
Quiz
❍
Exercises
❍
●
Answers for Day 14, "Scalar-Conversion and List-Manipulation Functions"
Quiz
❍
Exercises
❍
●
Answers for Day 15, "System Functions"
Quiz
❍
Exercises
❍
●
Answers for Day 16, "Command-Line Options"
●
Quiz
❍
Exercises
❍
Answers for Day 17, "System Variables"
Quiz
❍
Exercises
❍
●
Answers for Day 18, "References in Perl 5"
Quiz
❍
Exercises
❍
●
Answers for Day 19, "Object-Oriented Programming in Perl"
Quiz
❍
Exercises
❍
●
Answers for Day 20, "Miscellaneous Features of Perl"
Quiz
❍
Exercises
❍
●
Answers for Day 21, "The Perl Debugger"
Quiz
❍
●
Appendix B ASCII Character Set Credits
Copyright © 1996 by Sams Publishing SECOND EDITION
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein.
Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams Publishing, 201 W. 103rd St.,
Indianapolis, IN 46290.
International Standard Book Number: 0-672-30894-0 HTML conversion by :
M/s. LeafWriters (India) Pvt. Ltd.
Website : http://leaf.stpn.soft.net e-mail : leafwriters@leaf.stpn.soft.net
Publisher and President Richard K. Swadley Acquisitions Manager Greg Wiegand
Development Manager Dean Miller Managing Editor Cindy Morrow Marketing Manager John Pierce Assistant Marketing
Manager
Kristina Perry
Acquisitions Editor Chris Denny Development Editors Angelique Brittingham, Keith Davenport
Software Development Specialist
Steve Straiger Production Editor Tonya R. Simpson
Copy Editor Kimberly K. Hannel Technical Reviewer Elliotte Rusty Harold Editorial Coordinator Bill Whitmer Technical Edit
Coordinator
Lynette Quinn
Formatter Frank Sinclair Editorial Assistants Carol Ackerman, Andi
Richter Rhonda, Tinch-Mize
Cover Designer Tim Amrhein Book Designer Gary Adair
Copy Writer Peter Fuller Production Team
Supervisor
Brad Chinn
Production Michael Brumitt, Charlotte Clapp, Jason Hand, Sonja Hart, Louisa Klucznik, Ayanna Lacey, Clint Lahnen, Paula Lowell, Laura Robbins, Bobbi Satterfield, Carol Sheehan, Chris Wilcox
Acknowledgments
I would like to thank the following people for their help:
David Macklem at Sietec Open Systems for allowing me to take the time off to work on the first edition of this book
●
Everyone at Sams Publishing, for their efforts and encouragement
●
Jim Gardner, for telling the people at Sams Publishing about me
●
I'd also like to thank all those friends of mine (you know who you are) who tolerated my going stir-crazy as my deadlines approached.
About the Authors
David Till
David Till is a technical writer working in Toronto, Ontario, Canada. He holds a master's degree in computer science from the University of Waterloo; programming languages was his major field of study.
He also has worked in compiler development and on version-control software. He lists his hobbies as
"writing, comedy, walking, duplicate bridge, and fanatical support of the Toronto Blue Jays."
He can be reached via e-mail at
am671@freenet.toronto.on.caor
davet@klg.com, or on the World Wide Web at
http://www.interlog.com/~davet/.
Kamran Husain
Kamran Husain is a software consultant with experience in UNIX system programming. He has dabbled in
all sorts of software for real-time systems applications, telecommunications, seismic data acquisition and
navigation, X Window/Motif and Microsoft Windows applications. He refuses to divulge any more of his
qualifications. Kamran offers consulting services and training classes through his company, MPS Inc., in Houston, Texas. He is an alumnus of the University of Texas at Austin.
You can reach Kamran through Sams Publishing or via e-mail at
khusain@neosoft.comor
mpsi@aol.com.
Introduction
This book is designed to teach you the Perl programming language in just 21 days. When you finish reading this book, you will have learned why Perl is growing rapidly in popularity: It is powerful enough to perform many useful, sophisticated programming tasks, yet it is easy to learn and use.
Who Should Read This Book?
No previous programming experience is required for you to learn everything you need to know about programming with Perl from this book. In particular, no knowledge of the C programming language is required. If you are familiar with other programming languages, learning Perl will be a snap. The only assumption this book does make is that you are familiar with the basics of using the UNIX operating system.
Special Features of This Book
This book contains some special elements that help you understand Perl features and concepts as they are introduced:
Syntax boxes
●
DO/DON'T boxes
●
Notes
●
Warnings
●
Tips
●
Syntax boxes explain some of the more complicated features of Perl, such as the control structures. Each
syntax box consists of a formal definition of the feature followed by an explanation of the elements of the feature. Here is an example of a syntax box:
The syntax of the
forstatement is
for (expr1; expr2; expr3) { statement_block }
expr1
is the loop initializer. It is evaluated only once, before the start of the loop.
expr2
is the conditional expression that terminates the loop. The conditional expression in
expr2behaves
just like the ones in
whileand
ifstatements: If its value is zero, the loop is terminated, and if its value is nonzero, the loop is executed.
statement_block is the collection of statements that is executed if (and when) expr2 has a nonzero
value.
expr3
is executed once per iteration of the loop, and is executed after the last statement in
statement_blockis executed.
Don't try to understand this definition yet!
DO/DON'T boxes present the do's and don'ts for a particular task or feature. Here is an example of such a
box:
DON'T confuse the |
operator (bitwise OR) with the
||operator (logical OR).
DO make sure you are using the proper bitwise operator. It's easy to slip and assume you
want bitwise OR when you really want bitwise AND. (Trust me.
Notes are explanations of interesting properties of a particular program feature. Here is an example of a
note:
NOTE
In left-justified output, the value being displayed appears at the left end of the value field. In right-justified output, the value being displayed appears at the right end of the value field.
Warnings warn you of programming pitfalls to avoid. Here is a typical warning:
You cannot use the last statement inside the do statement. The
dostatement, although it behaves like the other control structures, is actually implemented differently.
Tips are hints on how to write your Perl programs better. Here is an example of a tip:
TIP
It is a good idea to use all uppercase letters for your file variable names. This makes it easier to distinguish file variable names from other variable names and from reserved words.
Programming Examples
Each feature of Perl is illustrated by examples of its use. In addition, each chapter of this book contains many useful programming examples complete with explanations; these examples show you how you can use Perl features in your own programs.
Each example contains a listing of the program, the input required by and the output generated by the
program, and an analysis of how the program works. Special icons are used to point out each part of the
example: Type, Input-Output, and Analysis.
In the Input-Output example following Listing IN.1, there are some special typographic conventions. The input you enter is shown in
bold monospacetype, and the output generated by the system or the program is shown in
plain monospacetype. The system prompt (
$in the examples in this book) is shown so that you know when a command is to be entered on the command line.
Listing IN.1. A simple Perl program with comments.
1: #!/usr/local/bin/perl
2: # this program reads a line of input, and writes the line 3: # back out
4: $inputline = <STDIN>; # read a line of input 5: print( $inputline ); # write the line out
$ programIN_1
This is a line of input.
This is a line of input.
$
Line 1 is the header comment. Lines 2 and 3 are comments, not executable lines of code. Line 4 reads a line of input. Line 5 writes the line of input on your screen.
End-of-Day Q& A and Workshop
Each day ends with a Q&A section containing answers to common questions relating to that day's material.
There also is a Workshop at the end of each day that consists of quiz questions and programming exercises.
The exercises often include BUG BUSTER exercises that help you spot some of the common bugs that crop up in Perl programs. The answers to these quiz questions as well as sample solutions for the exercises are presented in Appendix A, "Answers."
Conventions Used in This Book
This book uses different typefaces to help you differentiate between Perl code and regular English, and also to help you identify important concepts.
Actual Perl code is typeset in a special
monospacefont. You'll see this font used in listings and the
●
Input-Output examples, as well as in code snippets. In the explanations of Perl features, commands, filenames, statements, variables, and any text you see on the screen also are typeset in this font.
Command input and anything that you are supposed to enter appears in a
bold monospacefont.
You'll see this mainly in the Input-Output examples.
●
Placeholders in syntax descriptions appear in an
italicmonospacefont. Replace the placeholder with the actual filename, parameter, or whatever element it represents.
●
Italics highlight technical terms when they first appear in the text and are sometimes used to
emphasize important points.
●
What You'll Learn in 21 Days
In your first week of learning Perl, you'll learn enough of the basics of Perl to write many useful Perl programs. Here's a summary of what you'll learn in Week 1:
Day 1, "Getting Started," tells you how to get Perl, how to run Perl programs, and how to
read from your keyboard and write to your screen.
Day 2, "Basic Operators and Control Flow," teaches you about simple arithmetic, how to
assign a value to a scalar variable, and how to control execution using conditional statements.
Day 3, "Understanding Scalar Values," teaches you about integers, floating-point numbers,
and character strings. It also shows you that all three are interchangeable in Perl.
Day 4, "More Operators," tells you all about operators and expressions in Perl and talks
about operator associativity and precedence.
Day 5, "Lists and Array Variables," introduces you to lists, which are collections of values,
and to array variables, which store lists.
Day 6, "Reading from and Writing to Files," tells you how to interact with your file system
by reading from input files, writing to output files, and testing for particular file attributes.
Day 7, "Pattern Matching," describes pattern-matching in Perl and shows how you can
substitute values and translate sets of characters in text strings.
By the end of Week 2, you'll have mastered almost all the features of Perl; you'll also have learned about many of the library functions supplied with the language. Here's a summary of what you'll learn:
Day 8, "More Control Structures," discusses the control flow statements not previously
covered.
Day 9, "Using Subroutines," shows how you can break your program into smaller, more
manageable, chunks.
Day 10, "Associative Arrays," introduces one of the most powerful and useful constructs in
Perl-arrays-and it shows how you can use these arrays to simulate other data structures.
Day 11, "Formatting Your Output," shows how you can use Perl to produce tidy reports.
Day 12, "Working with the File System," shows how you can interact with your system's
directory structure.
Day 13, "Process, String, and Mathematical Functions," describes the library functions that
interact with processes running on the system. It also describes the functions that perform trigonometric and other mathematical operations, and the functions that operate on strings.
Day 14, "Scalar-Conversion and List-Manipulation Functions," describes the library
functions that convert values from one form to another and the functions that work with lists and array variables.
By the end of Week 3, you'll know all the features and capabilities of Perl. It covers the rest of the Perl library functions and describes some of the more esoteric concepts of the language. Here's a summary of what you'll learn:
Day 15, "System Functions," describes the functions that manipulate the Berkeley UNIX and
UNIX System V environments.
Day 16, "Command-Line Options," describes the options you can supply with Perl to control
how your program runs.
Day 17, "System Variables," describes the built-in variables that are included automatically
as part of every Perl program.
Day 18, "References in Perl 5," describes the pointer and reference features of Perl 5,
including multi-dimensional arrays.
Day 19, "Object-Oriented Programming in Perl," describes the object-oriented capabilities
added to Perl 5. These enable you to hide information and divide your program into individual file modules.
Day 20, "Miscellaneous Features of Perl," covers some of the more exotic or obscure
features of the language.
Day 21, "The Perl Debugger," shows you how to use the Perl debugger to discover errors
quickly.
Week 1
Week at a Glance
CONTENTS
Where You're Going
●