Minimal Perl: For UNIX and Linux People
內容描述
Description
"If you are a Unix/Linux user and wish to learn
Perl, I recommend this book." -- George Wooley, Camelot.pm and
Oakland.pm
"This book is not perl tapas. It is a survival
tool." -- William M. Julien, HPC Unix Performance Analyst, Fortune 100
Company
"No-nonsense and practical, yet with wit and
charm. A joy to read." -- Dan Sanderson, Software Developer
"Shows style, not just facts, valuable." --
Brian Downs, former Training Director, Lucent Technologies
"Brilliant, never tedious, highly recommended!"
-- Jon Allen, Maintainer of perldoc.perl.org
"You could have chosen no better primer than this
book." -- Damian Conway, from the Foreword
Perl is a complex language that can be difficult
to master. Perl advocates boast that "There's More Than One Way To Do It", but
do you really want to learn several ways of saying the same thing to a
computer?
To make Perl more accessible, Dr. Tim Maher has
over the years designed and taught an essential subset of the language that is
smaller, yet practical and powerful. With this engaging book you can now
benefit from "Minimal Perl", even if all you know about Unix is
grep.
You will learn how to write simple Perl commands
(many just one-liners) that go far beyond the limitations of Unix utilities,
and those of Linux, MacOS/X, etc. And you雍l acquire the more advanced Perl
skills used in scripts by capitalizing on your knowledge of related Shell
resources. Sprinkled throughout are many Unix-specific Perl tips.
This book is especially suitable for system
administrators, webmasters, and software developers.
Table of
Contents
foreword preface acknowledgments
about this book about the cover illustration list of tables
Minimal Perl: for UNIX and Linux Users
1 Introducing Minimal Perl
A visit to Perlistan
Sometimes you need a professional guide
Perl can be simple
About Minimal Perl
What Minimal Perl isn't, What Minimal Perl
is
Laziness is a virtue
A minimal dose of syntax
Terminating statements with semicolons
Writing one-line programs
Balancing simplicity and readability,
Implementing simple filters
Summary
2 Perl essentials
Perl's invocation options
One-line programming: -e, Enabling warnings:
-w, Processing input: -n, Processing input with automatic printing: -p,
Processing line-endings: -l, Printing without newlines: printf, Changing
the input record separator: -0digits
Using variables
Using special variables, Using the data
variable: $_, Using the record-number variable: $., Employing user-defined
variables
Loading modules -M
Writing simple scripts
Quoting techniques, True and False values,
Handling switches: -s, Using warn and die, Using logical and, logical or,
Programming with BEGINand END blocks, Loading modules with use
Additional special variables
Employing I/O variables, Exploiting
formatting variables
Standard option clusters
Using aliases for common types of Perl
commands
Constructing programs
Constructing an output-only one-liner,
Constructing an input/output script
Summary
Directions for further study
3 Perl as a (better) grep command
A brief history of grep
Shortcomings of grep
Uncertain support for metacharacters, Lack
of string escapes for control characters, Comparing capabilities of
greppers and Perl
Working with the matching operator
The one-line Perl grepper
Understanding Perl's regex notation
Perl as a better fgrep
Displaying the match only, using $&
Displaying unmatched records (like grep -v),
Validating data, Minimizing typing with shortcut metacharacters
Displaying filenames only (like grep -l)
Using matching modifiers
Ignoring case (like grep -i)
Perl as a better egrep
Working with cascading filters
Matching in context
Paragraph mode, File mode
Spanning lines with regexes
Matching across lines, Using lwp-request,
Filtering lwp-request output
Additional examples
Log-file analysis, A scripted grepper, Fuzzy
matching, Web scraping
Summary
Directions for further study
4 Perl as a (better) sed command
A brief history of sed
Shortcomings of sed
Performing substitutions
Performing line-specific substitutions: sed,
Performing line-specific substitutions: Perl, Performing record-specific
substitutions: Perl, Using backreferences and numbered variables in
substitutions
Printing lines by number
Printing lines by number: sed, Printing
lines by number: Perl, Printing records by number: Perl
Modifying templates
Converting special characters
Editing files
Editing with commands, Editing with scripts,
Safeguarding in-place editing
Converting to lowercase or uppercase
Quieting spam
Substitutions with computed replacements
Converting miles to kilometers,
Substitutions using function results
The sed to Perl translator
Summary
Directions for further study
5 Perl as a (better) awk command
A brief history of AWK
Comparing basic features of awk and Perl
Pattern-matching capabilities, Special
variables, Perl's variable interpolation, Other advantages of Perl over
AWK, Summary of differences in basic features
Processing fields
Accessing fields, Printing fields,
Differences in syntax for print, Using custom field separators in Perl
Programming with Patterns and Actions
Combining pattern matching with field
processing, Extracting data from tables, Accessing cell data using array
indexing
Matching ranges of records
Operators for single- and multi-record
ranges, Matching a range of dates, Matching multiple ranges
Using relational and arithmetic operators
Relational operators, Arithmetic operators
Using built-in functions
One-liners that use functions, The legend of
nexpr, How the nexpr* programs work
Additional examples
Computing compound interest:
compound_interest, Conditionally pluralizing nouns: compound_interest2,
Analyzing log files: scan4oops
Using the AWK-to-Perl translator: a2p
Tips on using a2p
Summary
Directions for further study
6 Perl as a (better) find command
Introducing hybrid find / perl programs
File testing capabilities of find vs. Perl
Augmenting find with Perl
Finding files
Finding files by name matching, Finding
files by pathname matching
Processing filename arguments
Defending against grep's messes, Recursive
grepping, Perl as a generalized argument pre-processor
Using find | xargs vs. Perl alternatives
Using Perl for reliable timestamp sorting,
Dealing with multi-word filenames
find as an argument pre-processor for Perl
A Unix-like, OS-portable find command
Making the most of find2perl, Helping
non-Unix friends with find2perl
Summary
Directions for further study
Part 2 Minimal Perl: for UNIX and Linux Shell
Programmers
7 Built-in functions
Understanding and managing evaluation context
Determinants and effects of evaluation
context, Making use of evaluation context
Programming with functions that generate or
process scalars
Using split, Using localtime, Using stat,
Using chomp, Using rand
Programming with functions that process lists
Comparing Unix pipelines and Perl functions
, Using sort, Using grep, Using join, Using map
Globbing for filenames
Tips on globbing
Managing files with functions
Handling multi-valued return codes
Parenthesizing function arguments
Controlling argument-gobbling functions
Summary
Directions for further study
8 Scripting techniques
Exploiting script-oriented functions
Defining defined, Exiting with exit,
Shifting with shift
Pre-processing arguments
Accommodating non-filename arguments with
implicit loops, Filtering arguments, Generating arguments
Executing code conditionally with if/else
Employing if/else vs. and/or, Mixing
branching techniques: The cd_report script, Tips on using if/else
Wrangling strings with concatenation and
repetition operators
Enhancing the most_recent_file script, Using
concatenation and repetition operators together, Tips on using the
concatenation operator
Interpolating command output into source code
Using the tput command, Grepping
recursively: The rgrep script, Tips on using command interpolation
Executing OS commands using system
Generating reports, Tips on using system
Evaluating code using eval
Using a Perl shell: The psh script,
Appreciating a multi-faceted Perl grepper: The preg script
Summary
Directions for further study
9 List variables
Using array variables
Initializing arrays with piecemeal
assignments and push, Understanding advanced array indexing, Extracting
fields in a friendlier fashion, Telling fortunes: The fcookie script, Tips
on using arrays
Using hash variables
Initializing hashes, Understanding advanced
hash indexing, Understanding the built-in %ENV hash, Printing hashes,
Using %ENV in place of switches, Obtaining uniqueness with hashes,
Employing a hash as a simple database: The user_lookup script, Counting
word frequencies in web pages: The count_words script
Comparing list generators in the Shell and
Perl
Filename generation/globbing, Command
substitution/interpolation, Variable substitution/interpolation
Summary
Directions for further study
10 Looping facilities
Looping facilities in the Shell and Perl
Looping with while / until
Totaling numeric arguments, Reducing the
size of an image, Printing key/value pairs from a hash using each,
Understanding the implicit loop
Looping with do while / until
Prompting for input
Looping with foreach
Unlinking files: the rm_files script,
Reading a line at a time, Printing a hash, Demystifying acronyms: The
expand_acronyms script, Reducing image sizes: The compress_image2 script
Looping with for
Exploiting for's support for indexing: the
raffle script
Using loop-control directives
Nesting loops within loops, Enabling
loop-control directives in bottom-tested loops, Prompting for input,
Enhancing loops with continue blocks: the confirmation script
The CPAN's select loop for Perl
Avoiding the re-invention of the
"choose-from-a-menu" wheel, Monitoring user activity: the show_user
script, Browsing man pages: the perlman script
Summary
Directions for further study
11 Subroutines and variable scoping
Compartmentalizing code with subroutines
Defining and using subroutines,
Understanding use strict
Common problems with variables
Clobbering variables: The phone_home script,
Masking variables: The 4letter_word script, Tips on avoiding problems with
variables
Controlling variable scoping
Declaring variables with my , Declaring
variables with our , Declaring variables with local, Introducing the
Variable Scoping Guidelines
Variable Scoping Guidelines for complex
programs
Enable use strict, Declare user-defined
variables and define their scopes, Pass data to subroutines using
arguments, Localize temporary changes to built-in variables with local,
Employ user-defined loop variables ,Applying the Guidelines: the
phone_home2 script
Reusing a subroutine
Summary
Directions for further study
12 Modules and the CPAN
Creating modules
Using the Simple Module Template, Creating a
module: Center.pm, Testing a new module
Managing modules
Identifying the modules that you want,
Determining whether you have a certain module, Installing modules from the
CPAN
Using modules
Business::UPS-the ups_shipping_price script,
LWP::Simple-the check_links script, Shell::POSIX::Select-the menu_ls
script, File::Find-the check_symlinks script, CGI-the survey.cgi script,
Tips on using Object-Oriented modules
Summary
Directions for further study
epilogue 426 appendix A:
Perl special variables cheatsheet 427 appendix B: Guidelines for
parenthesizing code 430 glossary 432 index 443