INTRODUCTION xxiii
CHAPTER 1: WHAT IS PERL? 1
Perl Today 2
Getting Perl 3
Working with Non-Windows Platforms: perlbrew 4
Using Windows 6
The Perl Community 8
IRC 8
PerlMonks 9
Perl Mongers 9
StackOverflow 9
Using perldoc 11
Understanding the Structure of perldoc 11
Getting Started with perldoc 11
Using Tutorials and FAQs 12
Using the perldoc -f function 14
Using a Terminal Window 14
Using the Command Line 15
Creating a Work Directory 16
Creating Hello, World! 18
Writing Your First Program 18
Shebang Lines 21
Summary 22
CHAPTER 2: UNDERSTANDING THE CPAN 25
CPAN and METACPAN 26
Finding and Evaluating Modules 27
Downloading and Installing 29
CPAN Clients 33
Using the CPAN.pm Client 33
Using the Cpanm Client 35
PPM 36
CPAN::Mini 36
Summary 39
CHAPTER 3: VARIABLES 41
What Is Programming? 42
A Few Things to Note Before Getting Started 43
strict, warnings, and diagnostics 43
The my Function 43
Sigils 44
Identifiers 45
Scalars 46
Strings 47
Numbers 51
Arrays 53
Breaking Down the Code 54
Accessing Elements 55
Iterating over Arrays 58
Hashes 58
Accessing Elements 59
Iterating Over Hashes 60
Adding Data to Hashes 60
Slices 61
Array Slices 62
Hash Slices 62
Context 63
Scalar Context 63
List Context 64
Scope 67
my Variables 67
Package Variables 69
Strict, Warnings, and Diagnostics 72
strict 74
warnings 74
diagnostics 75
Working Without a Net 76
Perl’s Built-in Variables 78
$_ 78
%ENV 79
@ARGV 79
Other Special Variables 80
Summary 81
CHAPTER 4: WORKING WITH DATA 83
Using Scalars 84
Working with Strings 85
Using String Operators 94
Scalar::Util 97
Numeric Builtins 98
Bitwise Operators 103
Understanding Booleans 103
Assignment Operators 108
Precedence and Associativity 109
Array and List Functions 111
Built-in Array Functions 111
List::Util 116
Built-in Hash Functions 116
delete() 116
exists() 117
keys() 117
values() 117
each() 117
Scoping Keywords 119
my() 119
local() 119
our() 120
state() 120
Summary 121
CHAPTER 5: CONTROL FLOW 125
Using the if Statement 126
Understanding Basic Conditionals 126
else/elsif/unless 128
The Ternary Operator ?: 131
for/foreach loops 132
Arrays 132
Lists 135
C-Style 136
Using while/until Loops 142
Lists 143
last/next/redo/continue 144
Labels 146
Statement Modifiers 147
Types of Statement Modifiers 147
do while/do until 149
given/when 151
Basic Syntax 151
The Switch Module 153
Summary 154
CHAPTER 6: REFERENCES 157
References 101 158
Array References 158
Hash References 159
Anonymous References 160
Other References 163
Working with References 166
Debugging 166
Copying 169
Slices 172
Summary 173
CHAPTER 7: SUBROUTINES 175
Subroutine Syntax 176
Argument Handling 177
Multiple Arguments 178
Named Arguments 179
Aliasing 181
State Variables (Pre- and Post-5.10) 181
Passing a List, Hash, or Hashref? 184
Returning Data 186
Returning True/False 186
Returning Single and Multiple Values 188
wantarray 189
FAIL! 190
“Wake Up! Time to Die!” 191
carp and croak 192
eval 192
evalGotchas 194
Try::Tiny 195
Subroutine References 196
Existing Subroutines 196
Anonymous Subroutines 197
Closures 197
Prototypes 200
Argument Coercion 200
More Prototype Tricks 202
Mimicking Builtins 204
Forward Declarations 206
Prototype Summary 207
Recursion 209
Basic Recursion 209
Divide and Conquer 210
Memoization 211
Things to Watch For 215
Argument Aliasing 215
Scope Issues 216
Doing Too Much 216
Too Many Arguments 217
Summary 217
CHAPTER 8: REGULAR EXPRESSIONS 219
Basic Matching 220
Quantifiers 221
Escape Sequences 223
Extracting Data 226
Modifiers and Anchors 228
Character Classes 231
Grouping 232
Advanced Matching 235
Substitutions 235
Lookahead/Lookbehind Anchors 236
Named Subexpressions (5.10) 238
Common Regular Expression Issues 241
Regexp::Common 241
E-mail Addresses 242
HTML 242
Composing Regular Expressions 243
Summary 245
CHAPTER 9: FILES AND DIRECTORIES 249
Basic File Handling 250
Opening and Reading a File 250
File Test Operators 258
The Diamond Operator 260
Temporary Files 260
DATA as a File 261
binmode 262
Directories 265
Reading Directories 265
Globbing 265
Unicode 266
What Is Unicode? 267
Two Simple Rules 267
Lots of Complicated Rules 271
Useful Modules 276
File::Find 276
File::Path 278
File::Find::Rule 279
Summary 284
CHAPTER 10: SORT, MAP, AND GREP 287
Basic Sorting 288
Sorting Alphabetically 288
Sorting Numerically 289
Reverse Sorting 290
Complex Sort Conditions 290
Writing a sort Subroutine 292
Sorting and Unicode Fun! 293
map and grep 297
Using grep 298
Using map 303
Aliasing Issues 305
Trying to Do Too Much 306
Trying to Be Clever 307
Putting It All Together 308
Schwartzian Transform (aka decorate, sort, undecorate) 308
Guttman-Rosler Transform 310
Summary 311
CHAPTER 11: PACKAGES AND MODULES 315
Namespaces and Packages 316
use Versus require 321
Package Variables 323
Version Numbers 326
Subroutines in Other Packages 327
Exporting 327
Naming Conventions 330
BEGIN, UNITCHECK, CHECK, INIT, and END 335
BEGIN blocks 336
END Blocks 337
INIT, CHECK, and UNITCHECK Blocks 337
Plain Old Documentation (POD) 338
Documentation Structure 340
Headings 340
Paragraphs 341
Lists 341
Verbatim 342
Miscellaneous 342
Creating and Installing Modules 344
Creating a Simple Module 344
Makefile.PL or Module::Build? 349
Summary 349
CHAPTER 12: OBJECT ORIENTED PERL 353
What Are Objects? The Ævar the Personal Shopper 354
Three Rules of Perl OO 355
Class Is a Package 355
An Object Is a Reference That Knows Its Class 356
A Method Is a Subroutine 358
Objects – Another View 371
Using TV::Episode 371
Subclassing 374
Using TV::Episode::Broadcast 375
Class Versus Instance Data 379
A Brief Recap 381
Overloading Objects 381
Using UNIVERSAL 385
Understanding Private Methods 387
Gotchas 393
Unnecessary Methods 393
“Reaching Inside” 394
Multiple Inheritance 394
Summary 397
CHAPTER 13: MOOSE 399
Understanding Basic Moose Syntax 400
Using Attributes 402
Using Constructors 405
Understanding Inheritance 408
Taking Care of Your Moose 409
Advanced Moose Syntax 413
Using Type Constraints 414
Using Method Modifiers 417
Understanding and Using Roles 420
Exploring MooseX 425
Rewriting Television::Episode 428
Moose Best Practices 433
Use namespace::autoclean and Make Your Class Immutable 434
Never Override new() 434
Always Call Your Parent BUILDARGS Method 434
Provide Defaults if an Attribute is Not Required 434
Default to Read-Only 434
Put Your Custom Types in One Module and Give Them a Namespace 435
Don’t Use Multiple Inheritance 435
Always Consume All Your Roles at Once 435
Summary 436
CHAPTER 14: TESTING 439
Basic Tests 440
Using Test::More 440
Writing Your Tests 442
Understanding the prove Utility 443
Understanding Test::More Test Functions 444
Using ok 445
Using is 445
Using like 448
Using is_deeply 449
Using SKIP 450
Using TODO 450
Using eval {} 451
Using use_ok and require_ok 452
Working with Miscellaneous Test Functions 453
Using Other Testing Modules 457
Using Test::Diff erences 457
Using Test::Exception 459
Using Test::Warn 460
Using Test::Most 460
Understanding xUnit Style Using Testing 461
Using Test::Class 461
A Basic Test Class 463
Extending a Test Class 467
Using Test Control Methods 471
Calling Parent Test Control Methods 473
Summary 477
CHAPTER 15: THE INTERWEBS 481
A Brief Introduction to HTTP 482
Plack 484
Hello, World! 484
Handling Parameters 490
Templates 492
Handling POST Requests 496
Sessions 500
Web Clients 511
Extracting Links from Web Pages 512
Extracting Comments from Web Pages 514
Filling Out Forms Programmatically 515
Summary 520
CHAPTER 16: DATABASES 523
Using the DBI 524
Connecting to a Database 524
Using SQLite 527
Using DBD::SQLite 527
Selecting Basic Data 533
Using SELECT Statements 533
Using Bind Parameters 536
Inserting and Updating Data 539
Creating Transactions 540
Handling Errors 541
Summary 542
CHAPTER 17: PLAYS WELL WITH OTHERS 545
The Command Line 546
Reading User Input 546
Handling Command-Line Arguments 548
perlrun 551
Other Programs 556
Running an External Program 556
Reading Another Program’s Output 559
Writing to Another Program’s Input 560
STDERR 562
Summary 565
CHAPTER 18: COMMON TASKS 567
Using CSV Data 568
Reading CSV Data 569
Writing CSV Data 570
Understanding Basic XML 571
Reading CSV Data 572
Writing CSV Data 576
Handling Dates 580
Using the DateTime Module 580
Using Date::Tiny and DateTime::Tiny 581
Understanding Your Program 587
Using the Debugger 587
Profiling 594
Perl::Critic 604
Summary 608
CHAPTER 19: THE NEXT STEPS 611
What Next? 612
What This Book Covers 612
What This Book Leaves Out 613
Understanding Object-Relational Mappers 613
Understanding DBIx::Class 614
Understanding Basic DBIx::Class Usage 614
Understanding the Pros and Cons of an ORM 618
Using DBIx::Class::Schema::Loader 624
Using the Template Toolkit 625
Why Use Templates? 625
An Introduction to Template Toolkit 626
Using Catalyst to Build Apps 634
The Beauty of MVC 635
Setting Up a Catalyst Application 635
Using Catalyst Views 641
Using Catalyst Models 643
Using Catalyst Controllers 646
CRUD: Create, Read, Update, and Delete 648
Summary 651
APPENDIX: ANSWERS TO EXERCISES 655
INDEX 695