Ebook banner rotater for Web Programming and Administration
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 Using
getc
Reading a Binary File Using
binmode
Directory-Manipulation Functions
The
mkdir
Function
The
chdir
Function
The
opendir
Function
The
closedir
Function
The
readdir
Function
The
telldir
and
seekdir
Functions
The
rewinddir
Function
The
rmdir
Function
File-Attribute Functions
File-Relocation Functions
Link and Symbolic Link Functions
File-Permission Functions
Miscellaneous Attribute Functions
Using DBM Files
The
dbmopen
Function
The
dbmclose
Function
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
sin
and
cos
Functions
The
atan2
Function
The
sqrt
Function
The
exp
Function
The
log
Function
The
abs
Function
The
rand
and
srand
Functions
String-Manipulation Functions
The
index
Function
The
rindex
Function
The
length
Function
Retrieving String Length Using
tr
The
pos
Function
The
substr
Function
The
study
Function
Case Conversion Functions
The
quotemeta
Function
The
join
Function
The
sprintf
Function
Summary
Q&A
Workshop
Quiz
Exercises
Day
14
Scalar-Conversion and List-Manipulation Functions
The
chop
Function
The
chomp
Function
The
crypt
Function
The
hex
Function
The
int
Function
The
oct
Function
The
oct
Function and Hexadecimal Integers
The
ord
and
chr
Functions
The
scalar
Function
The
pack
Function
The
pack
Function and C Data Types
The
unpack
Function
Unpacking Strings
Skipping Characters When Unpacking
The
unpack
Function and
uuencode
The
vec
Function
The
defined
Function
The
undef
Function
Array and List Functions
The
grep
Function
The
splice
Function
The
shift
Function
The
unshift
Function
The
push
Function
The
pop
Function
Creating Stacks and Queues
The
split
Function
The
sort
and
reverse
Functions
The
map
Function
The
wantarray
Function
Associative Array Functions
The
keys
Function
The
values
Function
The
each
Function
The
delete
Function
The
exists
Function
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
getgrent
Function
The
setgrent
and
endgrent
Functions
The
getgrnam
Function
The
getgrid
Function
The
getnetent
Function
The
getnetbyaddr
Function
The
getnetbyname
Function
The
setnetent
and
endnetent
Functions
The
gethostbyaddr
Function
The
gethostbyname
Function
The
gethostent
,
sethostent
, and
endhostent
Functions
The
getlogin
Function
The
getpgrp
and
setpgrp
Functions
The
getppid
Function
The
getpwnam
Function
The
getpwuid
Function
The
getpwent
Function
The
setpwent
and
endpwent
Functions
The
getpriority
and
setpriority
Functions
The
getprotoent
Function
The
getprotobyname
and
getprotobynumber
Functions
The
setprotoent
and
endprotoent
Functions
The
getservent
Function
The
getservbyname
and
getservbyport
Functions
The
setservent
and
endservent
Functions
The
chroot
Function
The
ioctl
Function
The
alarm
Function
Calling the System
select
Function
The
dump
Function
Socket-Manipulation Functions
The
socket
Function
The
bind
Function
The
listen
Function
The
accept
Function
The
connect
Function
The
shutdown
Function
The
socketpair
Function
The
getsockopt
and
setsockopt
Functions
The
getsockname
and
getpeername
Functions
The UNIX System V IPC Functions
IPC Functions and the
require
Statement
The
msgget
Function
The
msgsnd
Function
The
msgrcv
Function
The
msgctl
Function
The
shmget
Function
The
shmwrite
Function
The
shmread
Function
The
shmctl
Function
The
semget
Function
The
semop
Function
The
semctl
Function
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
-v
Option: Printing the Perl Version Number
The
-c
Option: Checking Your Syntax
The
-w
Option: Printing Warnings
Checking for Possible Typos
Checking for Redefined Subroutines
Checking for Incorrect Comparison Operators
The
-e
Option: Executing a Single-Line Program
The
-s
Option: Supplying Your Own Command-Line Options
The
-s
Option and Other Command-Line Arguments
The
-P
Option: Using the C Preprocessor
The C Preprocessor: A Quick Overview
The
-I
Option: Searching for C Include Files
The
-n
Option: Operating on Multiple Files
The
-p
Option: Operating on Files and Printing
The
-i
Option: Editing Files
Backing Up Input Files Using the
-i
Option
The
-a
Option: Splitting Lines
The
-F
Option: Specifying the Split Pattern
The
-0
Option: Specifying Input End-of-Line
The
-l
Option: Specifying Output End-of-Line
The
-x
Option: Extracting a Program from a Message
Miscellaneous Options
The
-u
Option
The
-U
Option
The
-S
Option
The
-D
Option
The
-T
Option: Writing Secure Programs
The
-d
Option: 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
eval
Error 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
$^X
Variable
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
@ARGV
Variable
The
@F
Variable
The
@INC
Variable
The
%INC
Variable
The
%ENV
Variable
The
%SIG
Variable
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
*variable
Operator 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
require
Function
The
require
Function and Subroutine Libraries
Using
require
to Specify a Perl Version
The
$#array
Variables
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
reset
Function
Using
reset
with 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
main
Package
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
l
command
The
-
Command
The
w
Command
The
//
and
??
Commands
The
S
Command
Stepping Through Programs
The
s
Command
The
n
Command
The
f
command
The Carriage-Return Command
The
r
Command
Displaying Variable Values
The
X
Command
The
V
Command
Breakpoints
The
b
Command
The
c
Command
The
L
Command and Breakpoints
The
d
and
D
Commands
Tracing Program Execution
Line Actions
The
a
Command
The
A
Command
The
<
and
>
Commands
Displaying Line Actions Using the
L
Command
Other Debugging Commands
Executing Other Perl Statements
The
H
Command: Listing Preceding Commands
The
!
Command: Executing Previous Commands
The
T
Command: Stack Tracing
The
p
Command: Printing an Expression
The
=
Command: Defining Aliases
Predefining Aliases
The
h
Command: Debugger Help
Summary