Minimal Perl : for UNIX and Linux people

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Perl is a great language, with an ingenious mentality underlying it, and a terrific user community gathered around it. Given these roperties, it’s no wonder it has become so popular. It would have been easy to write a truly “minimal” book on Perl by revealing so little of the language that nobody would have been able to do much with it. This isn’t that kind of book.

Author(s): Tim Maher
Publisher: Manning
Year: 2007

Language: English
Pages: 504
City: Greenwich, Conn

Minimal Perl......Page 1
contents......Page 10
foreword......Page 18
preface......Page 20
acknowledgments......Page 23
Audience and organization......Page 24
Reference value......Page 25
Entertainment value......Page 26
Essential terminology......Page 27
Constant width......Page 28
Markup for highlighting and cross-referencing......Page 29
Displays of commands or code with output......Page 30
Perl code-with-output displays......Page 31
Shell programs......Page 32
Errata......Page 33
About the authors......Page 34
Part 1 Minimal Perl: for UNIX and Linux Users......Page 40
1.1 A visit to Perlistan......Page 42
1.1.1 Sometimes you need a professional guide......Page 44
1.3 About Minimal Perl......Page 46
1.3.2 What Minimal Perl is......Page 47
1.4 Laziness is a virtue......Page 48
1.5.1 Terminating statements with semicolons......Page 49
1.6 Writing one-line programs......Page 50
1.6.2 Implementing simple filters......Page 51
1.7 Summary......Page 53
chapter 2 Perl essentials......Page 55
2.1 Perl’s invocation options......Page 56
2.1.2 Enabling warnings: -w......Page 57
2.1.4 Processing input with automatic printing: -p......Page 58
2.1.5 Processing line-endings: -l......Page 59
2.1.6 Printing without newlines: printf......Page 60
2.1.7 Changing the input record separator: -0digits......Page 61
2.2.1 Using special variables......Page 62
2.2.3 Using the record-number variable: $.......Page 63
2.2.4 Employing user-defined variables......Page 64
2.3 Loading modules: -M......Page 66
2.4 Writing simple scripts......Page 68
2.4.1 Quoting techniques......Page 69
2.4.3 Handling switches: -s......Page 71
2.4.4 Using warn and die......Page 74
2.4.5 Using logical and, logical or......Page 76
2.4.6 Programming with BEGIN and END blocks......Page 78
2.4.7 Loading modules with use......Page 80
2.5.1 Employing I/O variables......Page 81
2.5.2 Exploiting formatting variables......Page 82
2.6 Standard option clusters......Page 83
2.6.1 Using aliases for common types of Perl commands......Page 85
2.7 Constructing programs......Page 86
2.7.1 Constructing an output-only one-liner......Page 88
2.7.2 Constructing an input/output script......Page 89
Directions for further study......Page 90
3.1 A brief history of grep......Page 92
3.2.1 Uncertain support for metacharacters......Page 93
3.2.2 Lack of string escapes for control characters......Page 95
3.2.3 Comparing capabilities of greppers and Perl......Page 96
3.3 Working with the matching operator......Page 99
3.3.1 The one-line Perl grepper......Page 100
3.4 Understanding Perl’s regex notation......Page 102
3.6 Displaying the match only, using $&......Page 103
3.7 Displaying unmatched records (like grep -v)......Page 104
3.7.1 Validating data......Page 105
3.8 Displaying filenames only (like grep -l)......Page 106
3.9 Using matching modifiers......Page 107
3.10 Perl as a better egrep......Page 109
3.10.1 Working with cascading filters......Page 111
3.11.1 Paragraph mode......Page 114
3.12 Spanning lines with regexes......Page 116
3.12.1 Matching across lines......Page 118
3.12.3 Filtering lwp-request output......Page 119
3.13.1 Log-file analysis......Page 120
3.13.2 A scripted grepper......Page 123
3.13.3 Fuzzy matching......Page 124
3.14 Summary......Page 125
Directions for further study......Page 127
4.1 A brief history of sed......Page 128
4.2 Shortcomings of sed......Page 130
4.3 Performing substitutions......Page 132
4.3.2 Performing line-specific substitutions: Perl......Page 135
4.3.3 Performing record-specific substitutions: Perl......Page 136
4.3.4 Using backreferences and numbered variables in substitutions......Page 138
4.4.2 Printing lines by number: Perl......Page 139
4.5 Modifying templates......Page 140
4.6 Converting special characters......Page 142
4.7.1 Editing with commands......Page 144
4.7.2 Editing with scripts......Page 146
4.7.3 Safeguarding in-place editing......Page 150
4.8.1 Quieting spam......Page 152
4.9.1 Converting miles to kilometers......Page 153
4.9.2 Substitutions using function results......Page 155
4.11 Summary......Page 157
Directions for further study......Page 159
chapter 5 Perl as a (better) awk command......Page 160
5.1 A brief history of AWK......Page 161
5.2 Comparing basic features of awk and Perl......Page 162
5.2.1 Pattern-matching capabilities......Page 163
5.2.2 Special variables......Page 165
5.2.3 Perl’s variable interpolation......Page 167
5.2.5 Summary of differences in basic features......Page 168
5.3.1 Accessing fields......Page 169
5.3.2 Printing fields......Page 171
5.3.3 Differences in syntax for print......Page 173
5.3.4 Using custom field separators in Perl......Page 175
5.4 Programming with Patterns and Actions......Page 177
5.4.1 Combining pattern matching with field processing......Page 181
5.4.2 Extracting data from tables......Page 182
5.4.3 Accessing cell data using array indexing......Page 184
5.5 Matching ranges of records......Page 190
5.5.1 Operators for single- and multi-record ranges......Page 191
5.5.2 Matching a range of dates......Page 192
5.5.3 Matching multiple ranges......Page 194
5.6.1 Relational operators......Page 196
5.6.2 Arithmetic operators......Page 197
5.7 Using built-in functions......Page 198
5.7.1 One-liners that use functions......Page 200
5.7.2 The legend of nexpr......Page 201
5.7.3 How the nexpr* programs work......Page 203
5.8.1 Computing compound interest: compound_interest......Page 204
5.8.2 Conditionally pluralizing nouns: compound_interest2......Page 205
5.8.3 Analyzing log files: scan4oops......Page 207
5.10 Summary......Page 214
Directions for further study......Page 216
chapter 6 Perl as a (better) find command......Page 217
6.2 File testing capabilities of find vs. Perl......Page 219
6.2.1 Augmenting find with Perl......Page 222
6.3.1 Finding files by name matching......Page 223
6.3.2 Finding files by pathname matching......Page 226
6.4 Processing filename arguments......Page 227
6.4.1 Defending against grep’s messes......Page 228
6.4.2 Recursive grepping......Page 230
6.5 Using find | xargs vs. Perl alternatives......Page 231
6.5.1 Using Perl for reliable timestamp sorting......Page 232
6.5.2 Dealing with multi-word filenames......Page 235
6.6 find as an argument pre-processor for Perl......Page 236
6.7.1 Making the most of find2perl......Page 237
6.7.2 Helping non-Unix friends with find2perl......Page 238
6.8 Summary......Page 239
Directions for further study......Page 240
Part 2 Minimal Perl: for UNIX and Linux Shell Programmers......Page 242
chapter 7 Built-in functions......Page 244
7.1 Understanding and managing evaluation context......Page 245
7.1.1 Determinants and effects of evaluation context......Page 246
7.1.2 Making use of evaluation context......Page 247
7.2 Programming with functions that generate or process scalars......Page 249
7.2.1 Using split......Page 250
7.2.2 Using localtime......Page 253
7.2.3 Using stat......Page 254
7.2.4 Using chomp......Page 258
7.2.5 Using rand......Page 260
7.3.1 Comparing Unix pipelines and Perl functions......Page 262
7.3.2 Using sort......Page 263
7.3.3 Using grep......Page 266
7.3.4 Using join......Page 268
7.3.5 Using map......Page 271
7.4 Globbing for filenames......Page 273
7.4.1 Tips on globbing......Page 276
7.5 Managing files with functions......Page 278
7.5.1 Handling multi-valued return codes......Page 279
7.6.1 Controlling argument-gobbling functions......Page 281
7.7 Summary......Page 282
Directions for further study......Page 284
chapter 8 Scripting techniques......Page 286
8.1 Exploiting script-oriented functions......Page 287
8.1.1 Defining defined......Page 288
8.1.2 Exiting with exit......Page 292
8.1.3 Shifting with shift......Page 293
8.2.1 Accommodating non-filename arguments with implicit loops......Page 295
8.2.2 Filtering arguments......Page 296
8.3 Executing code conditionally with if/else......Page 298
8.3.1 Employing if/else vs. and/or......Page 299
8.3.2 Mixing branching techniques: The cd_report script......Page 300
8.3.3 Tips on using if/else......Page 303
8.4 Wrangling strings with concatenation and repetition operators......Page 304
8.4.2 Using concatenation and repetition operators together......Page 306
8.4.3 Tips on using the concatenation operator......Page 307
8.5 Interpolating command output into source code......Page 308
8.5.1 Using the tput command......Page 310
8.5.2 Grepping recursively: The rgrep script......Page 312
8.5.3 Tips on using command interpolation......Page 313
8.6 Executing OS commands using system......Page 314
8.6.1 Generating reports......Page 316
8.6.2 Tips on using system......Page 319
8.7 Evaluating code using eval......Page 322
8.7.1 Using a Perl shell: The psh script......Page 323
8.7.2 Appreciating a multi-faceted Perl grepper: The preg script......Page 325
8.8 Summary......Page 331
Directions for further study......Page 333
chapter 9 List variables......Page 334
9.1 Using array variables......Page 335
9.1.1 Initializing arrays with piecemeal assignments and push......Page 338
9.1.2 Understanding advanced array indexing......Page 339
9.1.3 Extracting fields in a friendlier fashion......Page 340
9.1.4 Telling fortunes: The fcookie script......Page 343
9.2 Using hash variables......Page 347
9.2.1 Initializing hashes......Page 350
9.2.2 Understanding advanced hash indexing......Page 351
9.2.3 Understanding the built-in %ENV hash......Page 352
9.2.4 Printing hashes......Page 353
9.2.5 Using %ENV in place of switches......Page 354
9.2.6 Obtaining uniqueness with hashes......Page 355
9.2.7 Employing a hash as a simple database: The user_lookup script......Page 358
9.2.8 Counting word frequencies in web pages: The count_words script......Page 362
9.3 Comparing list generators in the Shell and Perl......Page 364
9.3.1 Filename generation/globbing......Page 365
9.3.3 Variable substitution/interpolation......Page 366
9.4 Summary......Page 367
Directions for further study......Page 368
chapter 10 Looping facilities......Page 369
10.1 Looping facilities in the Shell and Perl......Page 370
10.2.1 Totaling numeric arguments......Page 372
10.2.2 Reducing the size of an image......Page 374
10.2.3 Printing key/value pairs from a hash using each......Page 375
10.2.4 Understanding the implicit loop......Page 376
10.3 Looping with do while / until......Page 377
10.3.1 Prompting for input......Page 378
10.4 Looping with foreach......Page 379
10.4.2 Reading a line at a time......Page 380
10.4.3 Printing a hash......Page 381
10.4.4 Demystifying acronyms: The expand_acronyms script......Page 382
10.4.5 Reducing image sizes: The compress_image2 script......Page 383
10.5 Looping with for......Page 384
10.5.1 Exploiting for’s support for indexing: the raffle script......Page 386
10.6 Using loop-control directives......Page 388
10.6.1 Nesting loops within loops......Page 389
10.6.2 Enabling loop-control directives in bottom-tested loops......Page 390
10.6.3 Prompting for input......Page 391
10.6.4 Enhancing loops with continue blocks: the confirmation script......Page 392
10.7 The CPAN’s select loop for Perl......Page 394
10.7.1 Avoiding the re-invention of the “choose-from-a-menu” wheel......Page 395
10.7.2 Monitoring user activity: the show_user script......Page 396
10.7.3 Browsing man pages: the perlman script......Page 397
10.8 Summary......Page 399
Directions for further study......Page 400
chapter 11 Subroutines and variable scoping......Page 401
11.1 Compartmentalizing code with subroutines......Page 402
11.1.1 Defining and using subroutines......Page 404
11.1.2 Understanding use strict......Page 407
11.2 Common problems with variables......Page 409
11.2.1 Clobbering variables: The phone_home script......Page 410
11.2.2 Masking variables: The 4letter_word script......Page 411
11.3 Controlling variable scoping......Page 412
11.3.2 Declaring variables with our......Page 413
11.3.4 Introducing the Variable Scoping Guidelines......Page 414
11.4 Variable Scoping Guidelines for complex programs......Page 416
11.4.2 Declare user-defined variables and define their scopes......Page 417
11.4.5 Employ user-defined loop variables......Page 423
11.4.6 Applying the Guidelines: the phone_home2 script......Page 424
11.5 Reusing a subroutine......Page 426
Directions for further study......Page 427
chapter 12 Modules and the CPAN......Page 428
12.1 Creating modules......Page 429
12.1.1 Using the Simple Module Template......Page 430
12.1.2 Creating a module: Center.pm......Page 433
12.1.3 Testing a new module......Page 435
12.2.1 Identifying the modules that you want......Page 438
12.2.2 Determining whether you have a certain module......Page 440
12.2.3 Installing modules from the CPAN......Page 441
12.3.1 Business::UPS-the ups_shipping_price script......Page 443
12.3.2 LWP::Simple-the check_links script......Page 445
12.3.3 Shell::POSIX::Select-the menu_ls script......Page 448
12.3.4 File::Find-the check_symlinks script......Page 451
12.3.5 CGI-the survey.cgi script......Page 454
12.3.6 Tips on using Object-Oriented modules......Page 462
12.4 Summary......Page 464
Directions for further study......Page 465
epilogue......Page 466
Perl special variables cheatsheet......Page 467
Guidelines for parenthesizing code......Page 470
glossary......Page 472
index......Page 483