Author(s): Tim Maher
Publisher: Manning
Year: 2005
Language: English
Pages: 495
brief contents......Page 5
contents......Page 6
foreword......Page 14
preface......Page 15
acknowledgments......Page 18
Audience and organization......Page 19
Reference value......Page 20
Entertainment value......Page 21
Essential terminology......Page 22
Constant width......Page 23
Markup for highlighting and cross-referencing......Page 24
Displays of commands or code with output......Page 25
Perl code-with-output displays......Page 26
Shell programs......Page 27
Errata......Page 28
About the authors......Page 29
about the cover illustration......Page 30
tables......Page 31
Part 1 Minimal Perl: for UNIX and Linux Users......Page 34
1.1 A visit to Perlistan......Page 36
1.1.1 Sometimes you need a professional guide......Page 38
1.3 About Minimal Perl......Page 40
1.3.2 What Minimal Perl is......Page 41
1.4 Laziness is a virtue......Page 42
1.5.1 Terminating statements with semicolons......Page 43
1.6 Writing one-line programs......Page 44
1.6.2 Implementing simple filters......Page 45
1.7 Summary......Page 47
chapter 2 Perl essentials......Page 49
2.1 Perl’s invocation options......Page 50
2.1.2 Enabling warnings: -w......Page 51
2.1.4 Processing input with automatic printing: -p......Page 52
2.1.5 Processing line-endings: -l......Page 53
2.1.6 Printing without newlines: printf......Page 54
2.1.7 Changing the input record separator: -0digits......Page 55
2.2.1 Using special variables......Page 56
2.2.3 Using the record-number variable: $.......Page 57
2.2.4 Employing user-defined variables......Page 58
2.3 Loading modules: -M......Page 60
2.4 Writing simple scripts......Page 62
2.4.1 Quoting techniques......Page 63
2.4.3 Handling switches: -s......Page 65
2.4.4 Using warn and die......Page 68
2.4.5 Using logical and, logical or......Page 70
2.4.6 Programming with BEGIN and END blocks......Page 72
2.4.7 Loading modules with use......Page 74
2.5.1 Employing I/O variables......Page 75
2.5.2 Exploiting formatting variables......Page 76
2.6 Standard option clusters......Page 77
2.6.1 Using aliases for common types of Perl commands......Page 79
2.7 Constructing programs......Page 80
2.7.1 Constructing an output-only one-liner......Page 82
2.7.2 Constructing an input/output script......Page 83
Directions for further study......Page 84
3.1 A brief history of grep......Page 86
3.2.1 Uncertain support for metacharacters......Page 87
3.2.2 Lack of string escapes for control characters......Page 89
3.2.3 Comparing capabilities of greppers and Perl......Page 90
3.3 Working with the matching operator......Page 93
3.3.1 The one-line Perl grepper......Page 94
3.4 Understanding Perl’s regex notation......Page 96
3.6 Displaying the match only, using $&......Page 97
3.7 Displaying unmatched records (like grep -v)......Page 98
3.7.1 Validating data......Page 99
3.8 Displaying filenames only (like grep -l)......Page 100
3.9 Using matching modifiers......Page 101
3.10 Perl as a better egrep......Page 103
3.10.1 Working with cascading filters......Page 105
3.11.1 Paragraph mode......Page 108
3.12 Spanning lines with regexes......Page 110
3.12.1 Matching across lines......Page 112
3.12.3 Filtering lwp-request output......Page 113
3.13.1 Log-file analysis......Page 114
3.13.2 A scripted grepper......Page 117
3.13.3 Fuzzy matching......Page 118
3.14 Summary......Page 119
Directions for further study......Page 121
4.1 A brief history of sed......Page 122
4.2 Shortcomings of sed......Page 124
4.3 Performing substitutions......Page 126
4.3.2 Performing line-specific substitutions: Perl......Page 129
4.3.3 Performing record-specific substitutions: Perl......Page 130
4.3.4 Using backreferences and numbered variables in substitutions......Page 132
4.4.2 Printing lines by number: Perl......Page 133
4.5 Modifying templates......Page 134
4.6 Converting special characters......Page 136
4.7.1 Editing with commands......Page 138
4.7.2 Editing with scripts......Page 140
4.7.3 Safeguarding in-place editing......Page 144
4.8.1 Quieting spam......Page 146
4.9.1 Converting miles to kilometers......Page 147
4.9.2 Substitutions using function results......Page 149
4.11 Summary......Page 151
Directions for further study......Page 153
chapter 5 Perl as a (better) awk command......Page 154
5.1 A brief history of AWK......Page 155
5.2 Comparing basic features of awk and Perl......Page 156
5.2.1 Pattern-matching capabilities......Page 157
5.2.2 Special variables......Page 159
5.2.3 Perl’s variable interpolation......Page 161
5.2.5 Summary of differences in basic features......Page 162
5.3.1 Accessing fields......Page 163
5.3.2 Printing fields......Page 165
5.3.3 Differences in syntax for print......Page 167
5.3.4 Using custom field separators in Perl......Page 169
5.4 Programming with Patterns and Actions......Page 171
5.4.1 Combining pattern matching with field processing......Page 175
5.4.2 Extracting data from tables......Page 176
5.4.3 Accessing cell data using array indexing......Page 178
5.5 Matching ranges of records......Page 184
5.5.1 Operators for single- and multi-record ranges......Page 185
5.5.2 Matching a range of dates......Page 186
5.5.3 Matching multiple ranges......Page 188
5.6.1 Relational operators......Page 190
5.6.2 Arithmetic operators......Page 191
5.7 Using built-in functions......Page 192
5.7.1 One-liners that use functions......Page 194
5.7.2 The legend of nexpr......Page 195
5.7.3 How the nexpr* programs work......Page 197
5.8.1 Computing compound interest: compound_interest......Page 198
5.8.2 Conditionally pluralizing nouns: compound_interest2......Page 199
5.8.3 Analyzing log files: scan4oops......Page 201
5.10 Summary......Page 208
Directions for further study......Page 210
chapter 6 Perl as a (better) find command......Page 211
6.2 File testing capabilities of find vs. Perl......Page 213
6.2.1 Augmenting find with Perl......Page 216
6.3.1 Finding files by name matching......Page 217
6.3.2 Finding files by pathname matching......Page 220
6.4 Processing filename arguments......Page 221
6.4.1 Defending against grep’s messes......Page 222
6.4.2 Recursive grepping......Page 224
6.5 Using find | xargs vs. Perl alternatives......Page 225
6.5.1 Using Perl for reliable timestamp sorting......Page 226
6.5.2 Dealing with multi-word filenames......Page 229
6.6 find as an argument pre-processor for Perl......Page 230
6.7.1 Making the most of find2perl......Page 231
6.7.2 Helping non-Unix friends with find2perl......Page 232
6.8 Summary......Page 233
Directions for further study......Page 234
Part 2 Minimal Perl: for UNIX and Linux Shell Programmers......Page 235
chapter 7 Built-in functions......Page 237
7.1 Understanding and managing evaluation context......Page 238
7.1.1 Determinants and effects of evaluation context......Page 239
7.1.2 Making use of evaluation context......Page 240
7.2 Programming with functions that generate or process scalars......Page 242
7.2.1 Using split......Page 243
7.2.2 Using localtime......Page 246
7.2.3 Using stat......Page 247
7.2.4 Using chomp......Page 251
7.2.5 Using rand......Page 253
7.3.1 Comparing Unix pipelines and Perl functions......Page 255
7.3.2 Using sort......Page 256
7.3.3 Using grep......Page 259
7.3.4 Using join......Page 261
7.3.5 Using map......Page 264
7.4 Globbing for filenames......Page 266
7.4.1 Tips on globbing......Page 269
7.5 Managing files with functions......Page 271
7.5.1 Handling multi-valued return codes......Page 272
7.6.1 Controlling argument-gobbling functions......Page 274
7.7 Summary......Page 275
Directions for further study......Page 277
chapter 8 Scripting techniques......Page 279
8.1 Exploiting script-oriented functions......Page 280
8.1.1 Defining defined......Page 281
8.1.2 Exiting with exit......Page 285
8.1.3 Shifting with shift......Page 286
8.2.1 Accommodating non-filename arguments with implicit loops......Page 288
8.2.2 Filtering arguments......Page 289
8.3 Executing code conditionally with if/else......Page 291
8.3.1 Employing if/else vs. and/or......Page 292
8.3.2 Mixing branching techniques: The cd_report script......Page 293
8.3.3 Tips on using if/else......Page 296
8.4 Wrangling strings with concatenation and repetition operators......Page 297
8.4.2 Using concatenation and repetition operators together......Page 299
8.4.3 Tips on using the concatenation operator......Page 300
8.5 Interpolating command output into source code......Page 301
8.5.1 Using the tput command......Page 303
8.5.2 Grepping recursively: The rgrep script......Page 305
8.5.3 Tips on using command interpolation......Page 306
8.6 Executing OS commands using system......Page 307
8.6.1 Generating reports......Page 309
8.6.2 Tips on using system......Page 312
8.7 Evaluating code using eval......Page 315
8.7.1 Using a Perl shell: The psh script......Page 316
8.7.2 Appreciating a multi-faceted Perl grepper: The preg script......Page 318
8.8 Summary......Page 324
Directions for further study......Page 326
chapter 9 List variables......Page 327
9.1 Using array variables......Page 328
9.1.1 Initializing arrays with piecemeal assignments and push......Page 331
9.1.2 Understanding advanced array indexing......Page 332
9.1.3 Extracting fields in a friendlier fashion......Page 333
9.1.4 Telling fortunes: The fcookie script......Page 336
9.2 Using hash variables......Page 340
9.2.1 Initializing hashes......Page 343
9.2.2 Understanding advanced hash indexing......Page 344
9.2.3 Understanding the built-in %ENV hash......Page 345
9.2.4 Printing hashes......Page 346
9.2.5 Using %ENV in place of switches......Page 347
9.2.6 Obtaining uniqueness with hashes......Page 348
9.2.7 Employing a hash as a simple database: The user_lookup script......Page 351
9.2.8 Counting word frequencies in web pages: The count_words script......Page 355
9.3 Comparing list generators in the Shell and Perl......Page 357
9.3.1 Filename generation/globbing......Page 358
9.3.3 Variable substitution/interpolation......Page 359
9.4 Summary......Page 360
Directions for further study......Page 361
chapter 10 Looping facilities......Page 362
10.1 Looping facilities in the Shell and Perl......Page 363
10.2.1 Totaling numeric arguments......Page 365
10.2.2 Reducing the size of an image......Page 367
10.2.3 Printing key/value pairs from a hash using each......Page 368
10.2.4 Understanding the implicit loop......Page 369
10.3 Looping with do while / until......Page 370
10.3.1 Prompting for input......Page 371
10.4 Looping with foreach......Page 372
10.4.2 Reading a line at a time......Page 373
10.4.3 Printing a hash......Page 374
10.4.4 Demystifying acronyms: The expand_acronyms script......Page 375
10.4.5 Reducing image sizes: The compress_image2 script......Page 376
10.5 Looping with for......Page 377
10.5.1 Exploiting for’s support for indexing: the raffle script......Page 379
10.6 Using loop-control directives......Page 381
10.6.1 Nesting loops within loops......Page 382
10.6.2 Enabling loop-control directives in bottom-tested loops......Page 383
10.6.3 Prompting for input......Page 384
10.6.4 Enhancing loops with continue blocks: the confirmation script......Page 385
10.7 The CPAN’s select loop for Perl......Page 387
10.7.1 Avoiding the re-invention of the “choose-from-a-menu” wheel......Page 388
10.7.2 Monitoring user activity: the show_user script......Page 389
10.7.3 Browsing man pages: the perlman script......Page 390
10.8 Summary......Page 392
Directions for further study......Page 393
chapter 11 Subroutines and variable scoping......Page 394
11.1 Compartmentalizing code with subroutines......Page 395
11.1.1 Defining and using subroutines......Page 397
11.1.2 Understanding use strict......Page 400
11.2 Common problems with variables......Page 402
11.2.1 Clobbering variables: The phone_home script......Page 403
11.2.2 Masking variables: The 4letter_word script......Page 404
11.3 Controlling variable scoping......Page 405
11.3.2 Declaring variables with our......Page 406
11.3.4 Introducing the Variable Scoping Guidelines......Page 407
11.4 Variable Scoping Guidelines for complex programs......Page 408
11.4.2 Declare user-defined variables and define their scopes......Page 409
11.4.5 Employ user-defined loop variables......Page 415
11.4.6 Applying the Guidelines: the phone_home2 script......Page 416
11.5 Reusing a subroutine......Page 418
Directions for further study......Page 419
chapter 12 Modules and the CPAN......Page 420
12.1 Creating modules......Page 421
12.1.1 Using the Simple Module Template......Page 422
12.1.2 Creating a module: Center.pm......Page 425
12.1.3 Testing a new module......Page 427
12.2.1 Identifying the modules that you want......Page 430
12.2.2 Determining whether you have a certain module......Page 432
12.2.3 Installing modules from the CPAN......Page 433
12.3.1 Business::UPS-the ups_shipping_price script......Page 435
12.3.2 LWP::Simple-the check_links script......Page 437
12.3.3 Shell::POSIX::Select-the menu_ls script......Page 440
12.3.4 File::Find-the check_symlinks script......Page 443
12.3.5 CGI-the survey.cgi script......Page 446
12.3.6 Tips on using Object-Oriented modules......Page 454
12.4 Summary......Page 456
Directions for further study......Page 457
epilogue......Page 458
Perl special variables cheatsheet......Page 459
Guidelines for parenthesizing code......Page 462
glossary......Page 464
how to use this index......Page 473
index......Page 474