9781118013847
index.html

Beginning Perl (Wrox)

Curtis “Ovid” Poe

Abstract

Perl is the ever-popular, flexible, open source programming language that has been called the programmers’ Swiss army knife. This book introduces Perl to both new programmers and experienced ones who are looking to learn a new language. In the tradition of the popular Wrox Beginning guides, it presents step-by-step guidance in getting started, a host of try-it-out exercises, real-world examples, and everything necessary for a Perl novice to start programming with confidence.

Beginning Perl provides the information and instruction you need to confidently get started with Perl.

This book is available for pre-order now at the Wrox website.


1. What’s Perl?
The History of Perl
Perl’s Humble Beginnings
Perl Today
Getting Perl
System Perl
perlbrew
Windows
The Community
IRC
PerlMonks
Perl Mongers
StackOverflow
Perldoc
The Structure of perldoc
Getting Started with perldoc
Tutorials and FAQs
perldoc -f function
Using a terminal
The Command Line
Creating a Work Directory
Hello, World!
Your First Program
Shebang lines
Summary
What You Learned in This Chapter
2. Understanding the CPAN
CPAN and METACPAN
Finding and Evaluating Modules
Downloading and installing
CPAN Clients
CPAN.pm
Cpanm
PPM
CPAN::Mini
Summary
What You Learned in This Chapter
3. Variables
What Is Programming?
A Few Things to Note Before Getting Started
strict, warnings, and diagnostics
The my Function
Sigils
Identifiers
Scalars
Strings
Numbers
Arrays
Breaking Down the Code Example
Accessing Elements
Iterating Over Arrays
Hashes
Accessing Elements
Iterating Over Hashes
Adding Data to Hashes
Slices
Array Slices
Hash Slices
Context
Scalar Context
List Context
Scope
my Variables
Package Variables
strict, warnings, and diagnostics
strict
warnings
diagnostics
Working Without a Net
Perl’s Built-in Variables
$_
%ENV
@ARGV
Other Special Variables
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers
Chapter 3 Exercise Answers
Answer to Question 1
Answer to Question 2
Answer to Question 3
4. Working With data
Builtins
Scalar
Array and list functions
map()
Hash functions
Scoping keywords
Exercises
What You Learned in This Chapter
Answers
Chapter 4 Exercise Answers
5. Control flow
The if statement
Basic conditionals
else/elsif/unless
The Ternary Operator ?:
for/foreach loops
Arrays
Lists
C-Style
while/until loops
Lists
last/next/redo/continue
Labels
Statement Modifiers
Types of Statement Modifiers
do while/do until
given/when
Basic syntax
The Switch Module
Summary
Exercises
What You Learned in This Chapter
Answers to exercises
6. References
References 101
Array references
Hash references
Anonymous references
Other references
Working with References
Debugging
Copying
Slices
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
7. Subroutines
Subroutine Syntax
Argument handling
Multiple Arguments
Named Arguments
Aliasing
state variables (pre- and post-5.10)
Passing a list, hash, or hashref?
Returning data
Returning true/false
Returning single and multiple values
wantarray
FAIL!
“Wake up! It’s time to die!”
carp and croak
eval
eval gotchas
Try::Tiny
Subroutine references
Existing subroutines
Anonymous subroutines
Closures
Prototypes
Argument coercion
More Prototype Tricks
Mimicking builtins
Forward Declarations
Prototype Summary
Recursion
Basic recursion
Divide and Conquer
Memoization
Things to watch for
Argument aliasing
Scope issues
Doing too much
Too many arguments
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to Exercises
8. Regular Expressions
Basic matching
Quantifiers
Escape Sequences
Extracting Data
Modifiers and Anchors
Character Classes
Grouping
Advanced matching
Substitutions
Lookahead/Lookbehind Anchors
Named Subexpressions (5.10)
Common Regular Expression Issues
Regexp::Common
Email Addresses
HTML
Composing regular expressions
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
9. Files and Directories
Basic file handling
Opening and reading a file
File test operators
The Diamond Operator
Temporary Files
DATA as a file
binmode
Directories
Reading directories
Globbing
Unicode
What is Unicode?
Two simple rules
Lots of complicated rules
Useful Modules
File::Find
File::Path
File::Find::Rule
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to Exercises
10. Sort, map and grep
Basic sorting
Sorting alphabetically
Sorting Numerically
Reverse sorting
Complex sort conditions
Writing a sort subroutine
Sorting and Unicode Fun!
Map and grep
Using grep
Using map
Aliasing issues
Trying to do too much
Trying To Be Clever
Putting it all together
Schwartzian Transform (a.k.a., decorate, sort, undecorate)
Guttman-Rosler Transform
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
11. Packages and Modules
Namespaces and packages
use versus require
Package variables
Version numbers
Subroutines in other packages
Exporting
Naming conventions
BEGIN, UNITCHECK, CHECK, INIT and END
BEGIN blocks
END blocks
INIT, CHECK and UNITCHECK blocks
Plain Old Documentation (POD)
Documentation Structure
Headings
Paragraphs
Lists
Verbatim
Miscellaneous
Creating and Installing
Creating a simple module
Makefile.PL or Module::Build?
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
12. Object Oriented Perl
What are objects?
Ævar the Personal Shopper
3 rules of Perl OO
Class is a package
An Object is a Reference That Knows Its Class
A Method is a Subroutine
Objects - Another View
TV::Episode
Subclassing
Class versus Instance data
A Brief Recap
Overloading Objects
UNIVERSAL
The can() Method
Private methods
Gotchas
Unnecessary methods
“Reaching inside”
Multiple Inheritance
Summary
Exercises
What You Learned In This Chapter
Answers to exercises
13. Moose
What is Moose?
Basic Syntax
Attributes
Constructors
Inheritance
Taking Care of Your Moose
Advanced Syntax
Type constraints
Method modifiers
Roles
Exploring MooseX
Rewriting Television::Episode
Moose Best Practices
Use namespace::autoclean and make your class immutable
Never override new
Always call your parent BUILDARGS method.
Provide defaults if an attribute is not required.
Default to read-only.
Put your custom types in one module and give them a namespace.
Don’t use multiple inheritance
Always consume all of your roles at once.
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
14. Testing
Basic tests
Test::More
Writing your tests
The prove utility
Test::More test functions
ok
is
like
is_deeply
SKIP
TODO
eval {}
use_ok and require_ok
Miscellaneous test functions
Other testing modules
Test::Differences
Test::Exception
Test::Warn
Test::Most
xUnit style testing
Test::Class
A basic test class
Extending a test class
Test control methods
Calling parent test control methods
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
15. The Interwebs
A brief introduction to HTTP
Plack
Hello, World!
Handling parameters
Templates
Handling POST requests
Sessions
Web Clients
Extracting Links from Web Pages
Extracting Comments from Web Pages
Filling Out Forms Programmatically
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
16. Databases
DBI
Connecting to a database
SQLite
DBD::SQLite
Basic data selection
SELECT statements
Bind parameters
Inserting and updating data
Transactions
Error handling
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
17. Plays well with others
The Command Line
Reading user input
Handling command-line arguments
perlrun
Other Programs
Running an external program
Reading another program’s output
Writing to another program’s input
STDERR
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
18. Common tasks
CSV data
Reading
Writing
Basic XML
Reading
Writing
Date Handling
DateTime
Date::Tiny and DateTime::Tiny
Understanding your program
Debugger
Profiling
Perl::Critic
Summary
Exercises
WHAT YOU LEARNED IN THIS CHAPTER
Answers to exercises
Site last updated on: July 5, 2012 at 11:41:08 AM PDT