Minimal Perl

Book description

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 "Mininal Perl" even if all you know about Unix is grep.

In Minimal Perl, 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'll 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.



About the Technology


About the Book


What's Inside
  • A simpler, yet still powerful Perl
  • Development of concise commands and flexible scripts
  • How to package custom software in reusable modules
  • How to exploit CPAN modules to avoid reinventing the wheel
  • Language features in tabular summaries
  • 100+ reusable programs for: system administration, web development (HTML, CGI, Forms), networking, databases, finance, text analysis, and more


About the Reader

This book is especially suitable for system administrators, webmasters, and software developers.



About the Author

Dr. Tim Maher's multi-decade career as a software professional includes stints at U.C. Berkeley as the Humanities Computer Consultant, at the University of Utah as a Professor of Computer Science, and at AT&T, Sun Microsystems, Hewlett Packard, and Consultix as a Course Developer/Lecturer on operating systems and programming languages. Along the way, he's taught UNIX, Linux, or Perl to many thousands of individuals—ranging from technology-phobic poets to corporate IT engineers. Tim founded Seattle's Perl Users Group, and served as its leader for six years. Many of its 400+ members contributed useful ideas to this book. In his spare time, he enjoys the natural beauty of the Pacific Northwest, where he lives.



Quotes
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, 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, Lucent Technologies

Brilliant, never tedious, highly recommended!
- Jon Allen, Maintainer of perldoc.perl.org

You could have chosen no better primer that this book.
- Damian Conway, from the Foreword

Table of contents

  1. Copyright
    1. Dedication
  2. Foreword
  3. Preface
    1. The History of Minimal Perl
    2. The Genesis of the Book
      1. A little help from my friend
      2. An auspicious weather non-event
  4. Acknowledgments
  5. About this Book
    1. Audience and Organization
      1. Part 1: Perl for UNIX and Linux users
      2. Part 2: Perl for UNIX and Linux Shell programmers
      3. Reference materials
    2. Author’s Approach
      1. Reference value
      2. Forewarned is forearmed
      3. Entertainment value
    3. Essential Terminology
    4. Typographical Conventions
      1. Italics
      2. Markup for highlighting and cross-referencing
      3. Special characters
      4. Shell and Perl comments
    5. Displays of Commands or Code With Output
      1. Shell command-with-output displays
      2. Perl code-with-output displays
      3. Ellipsis marks
    6. Coding Conventions
      1. Shell programs
      2. Perl programs
    7. Downloading the Source Code
    8. Default Invocation Options for Perl Examples
    9. Depiction of Optional Material
    10. Errata
    11. Author Online
    12. About the Authors
  6. About the Cover Illustration
  7. Tables
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
  8. 1. Minimal Perl: for UNIX and Linux Users
    1. 1. Introducing Minimal Perl
      1. 1.1. A visit to Perlistan
        1. 1.1.1. Sometimes you need a professional guide
      2. 1.2. Perl can be simple
      3. 1.3. About Minimal Perl
        1. 1.3.1. What Minimal Perl isn’t
          1. Is Minimal Perl a dumbed-down version of Perl?
          2. Is Minimal Perl a less capable Perl?
          3. Will learning Minimal Perl restrict my future options?
        2. 1.3.2. What Minimal Perl is
          1. How does Minimal Perl relate to Unix and standard Perl?
      4. 1.4. Laziness is a virtue
      5. 1.5. A minimal dose of syntax
        1. 1.5.1. Terminating statements with semicolons
      6. 1.6. Writing one-line programs
        1. 1.6.1. Balancing simplicity and readability
        2. 1.6.2. Implementing simple filters
      7. 1.7. Summary
    2. 2. Perl essentials
      1. 2.1. Perl’s invocation options
        1. 2.1.1. One-line programming: -e
        2. 2.1.2. Enabling warnings: -w
        3. 2.1.3. Processing input: -n
        4. 2.1.4. Processing input with automatic printing: -p
        5. 2.1.5. Processing line-endings: -l
        6. 2.1.6. Printing without newlines: printf
          1. Tip on using printf for prompting
        7. 2.1.7. Changing the input record separator: -0digits
      2. 2.2. Using variables
        1. 2.2.1. Using special variables
        2. 2.2.2. Using the data variable: $_
        3. 2.2.3. Using the record-number variable: $.
        4. 2.2.4. Employing user-defined variables
          1. Tips on employing user-defined variables
      3. 2.3. Loading modules: -M
      4. 2.4. Writing simple scripts
        1. 2.4.1. Quoting techniques
          1. Typical uses of quotes
            1. Single quotes
            2. Double quotes
            3. Backslashes
        2. 2.4.2. True and False values
        3. 2.4.3. Handling switches: -s
          1. A switch-driven line-numbering script: show_files
        4. 2.4.4. Using warn and die
          1. Using $0 with warn and die
        5. 2.4.5. Using logical and, logical or
          1. Tips on using logical and, logical or
        6. 2.4.6. Programming with BEGIN and END blocks
          1. Testing and setting variables in the BEGIN block
        7. 2.4.7. Loading modules with use
      5. 2.5. Additional special variables
        1. 2.5.1. Employing I/O variables
        2. 2.5.2. Exploiting formatting variables
          1. Tip on using formatting variables
          2. Congratulations!
      6. 2.6. Standard option clusters
        1. 2.6.1. Using aliases for common types of Perl commands
          1. Aliases for Perl commands: Line mode
          2. Aliases for Perl commands: Paragraph mode
      7. 2.7. Constructing programs
        1. 2.7.1. Constructing an output-only one-liner
        2. 2.7.2. Constructing an input/output script
      8. 2.8. Summary
        1. Directions for further study
    3. 3. Perl as a (better) grep command
      1. 3.1. A brief history of grep
      2. 3.2. Shortcomings of grep
        1. 3.2.1. Uncertain support for metacharacters
          1. Dealing with conflicting regex dialects
        2. 3.2.2. Lack of string escapes for control characters
        3. 3.2.3. Comparing capabilities of greppers and Perl
          1. Comparing regex dialects
          2. Contrasting match-related capabilities
          3. Appreciating additional enhancements
      3. 3.3. Working with the matching operator
        1. 3.3.1. The one-line Perl grepper
          1. Automatic skipping of directory files
          2. Nesting single quotes
      4. 3.4. Understanding Perl’s regex notation
      5. 3.5. Perl as a better fgrep
      6. 3.6. Displaying the match only, using $&
      7. 3.7. Displaying unmatched records (like grep -v)
        1. 3.7.1. Validating data
        2. 3.7.2. Minimizing typing with shortcut metacharacters
      8. 3.8. Displaying filenames only (like grep -l)
      9. 3.9. Using matching modifiers
        1. 3.9.1. Ignoring case (like grep -i)
      10. 3.10. Perl as a better egrep
        1. 3.10.1. Working with cascading filters
          1. Switching from alternation metacharacters to pipes
          2. Switching from egrep to Perl to gain efficiency
      11. 3.11. Matching in context
        1. 3.11.1. Paragraph mode
          1. Senator Quimby needs a Perl expert
        2. 3.11.2. File mode
      12. 3.12. Spanning lines with regexes
        1. 3.12.1. Matching across lines
        2. 3.12.2. Using lwp-request
        3. 3.12.3. Filtering lwp-request output
      13. 3.13. Additional examples
        1. 3.13.1. Log-file analysis
          1. Disqualifying undesirable matches
        2. 3.13.2. A scripted grepper
        3. 3.13.3. Fuzzy matching
        4. 3.13.4. Web scraping
      14. 3.14. Summary
        1. Directions for further study
    4. 4. Perl as a (better) sed command
      1. 4.1. A brief history of sed
      2. 4.2. Shortcomings of sed
      3. 4.3. Performing substitutions
        1. 4.3.1. Performing line-specific substitutions: sed
        2. 4.3.2. Performing line-specific substitutions: Perl
        3. 4.3.3. Performing record-specific substitutions: Perl
        4. 4.3.4. Using backreferences and numbered variables in substitutions
      4. 4.4. Printing lines by number
        1. 4.4.1. Printing lines by number: sed
        2. 4.4.2. Printing lines by number: Perl
        3. 4.4.3. Printing records by number: Perl
      5. 4.5. Modifying templates
      6. 4.6. Converting special characters
      7. 4.7. Editing files
        1. 4.7.1. Editing with commands
        2. 4.7.2. Editing with scripts
          1. The insert_contact_info script
          2. Adding commentary to a regex
        3. 4.7.3. Safeguarding in-place editing
          1. Clobber-proofing backup files in commands: $SECONDS
          2. Clobber-proofing backup files in scripts: $$
      8. 4.8. Converting to lowercase or uppercase
        1. 4.8.1. Quieting spam
      9. 4.9. Substitutions with computed replacements
        1. 4.9.1. Converting miles to kilometers
        2. 4.9.2. Substitutions using function results
      10. 4.10. The sed to Perl translator
      11. 4.11. Summary
        1. Directions for further study
    5. 5. Perl as a (better) awk command
      1. 5.1. A brief history of AWK
      2. 5.2. Comparing basic features of awk and Perl
        1. 5.2.1. Pattern-matching capabilities
          1. Stingy matching
          2. Record-separator matching
        2. 5.2.2. Special variables
        3. 5.2.3. Perl’s variable interpolation
        4. 5.2.4. Other advantages of Perl over AWK
        5. 5.2.5. Summary of differences in basic features
          1. Ways in which Perl is superior to AWK
          2. Ways in which AWK is superior to Perl
      3. 5.3. Processing fields
        1. 5.3.1. Accessing fields
        2. 5.3.2. Printing fields
          1. A rock-star biodata system (AWK)
          2. A rock-star biodata system (Perl)
        3. 5.3.3. Differences in syntax for print
        4. 5.3.4. Using custom field separators in Perl
          1. Enhancing the rock-star biodata system
      4. 5.4. Programming with Patterns and Actions
        1. 5.4.1. Combining pattern matching with field processing
        2. 5.4.2. Extracting data from tables
          1. Comparing cities for “mean annual precipitation”
        3. 5.4.3. Accessing cell data using array indexing
          1. Comparing cities for “days of significant rain”
          2. Comparing cities for “sogginess”
      5. 5.5. Matching ranges of records
        1. 5.5.1. Operators for single- and multi-record ranges
        2. 5.5.2. Matching a range of dates
        3. 5.5.3. Matching multiple ranges
      6. 5.6. Using relational and arithmetic operators
        1. 5.6.1. Relational operators
        2. 5.6.2. Arithmetic operators
      7. 5.7. Using built-in functions
        1. 5.7.1. One-liners that use functions
        2. 5.7.2. The legend of nexpr
          1. Born in a barroom wager: nexpr
          2. The nexpr_p script (Perl)
        3. 5.7.3. How the nexpr* programs work
      8. 5.8. Additional examples
        1. 5.8.1. Computing compound interest: compound_interest
        2. 5.8.2. Conditionally pluralizing nouns: compound_interest2
        3. 5.8.3. Analyzing log files: scan4oops
          1. Constructing a regex to match “Oops” reports
          2. The enhanced scan4oops2 script
      9. 5.9. Using the AWK-to-Perl translator: a2p
        1. 5.9.1. Tips on using a2p
      10. 5.10. Summary
        1. Directions for further study
    6. 6. Perl as a (better) find command
      1. 6.1. Introducing hybrid find/perl programs
      2. 6.2. File testing capabilities of find vs. Perl
        1. 6.2.1. Augmenting find with Perl
      3. 6.3. Finding files
        1. 6.3.1. Finding files by name matching
          1. Finding multi-word filenames
        2. 6.3.2. Finding files by pathname matching
      4. 6.4. Processing filename arguments
        1. 6.4.1. Defending against grep’s messes
          1. A screen-safe grepper: text_grep
        2. 6.4.2. Recursive grepping
        3. 6.4.3. Perl as a generalized argument pre-processor
      5. 6.5. Using find|xargs vs. Perl alternatives
        1. 6.5.1. Using Perl for reliable timestamp sorting
        2. 6.5.2. Dealing with multi-word filenames
      6. 6.6. find as an argument pre-processor for Perl
      7. 6.7. A Unix-like, OS-portable find command
        1. 6.7.1. Making the most of find2perl
        2. 6.7.2. Helping non-Unix friends with find2perl
      8. 6.8. Summary
        1. Directions for further study
  9. 2. Minimal Perl: for UNIX and Linux Shell Programmers
    1. 7. Built-in functions
      1. 7.1. Understanding and managing evaluation context
        1. 7.1.1. Determinants and effects of evaluation context
        2. 7.1.2. Making use of evaluation context
          1. Converting data types
      2. 7.2. Programming with functions that generate or process scalars
        1. 7.2.1. Using split
          1. Tips on using split
        2. 7.2.2. Using localtime
          1. Tips on using localtime
        3. 7.2.3. Using stat
          1. Emulating the Shell’s –nt operator
          2. Emulating ls with the listfile script
          3. Tips on using stat
        4. 7.2.4. Using chomp
          1. Tips on using chomp
        5. 7.2.5. Using rand
      3. 7.3. Programming with functions that process lists
        1. 7.3.1. Comparing Unix pipelines and Perl functions
        2. 7.3.2. Using sort
          1. Sorting randomly
          2. Tips on using sort
        3. 7.3.3. Using grep
        4. 7.3.4. Using join
          1. Matching against list variables
          2. Developing HTML documents with join: The fields2lists script
        5. 7.3.5. Using map
          1. Converting Celsius to Fahrenheit: The c2f script
          2. Tips on using map
      4. 7.4. Globbing for filenames
        1. 7.4.1. Tips on globbing
          1. Employing regex notation for filtering filenames
      5. 7.5. Managing files with functions
        1. 7.5.1. Handling multi-valued return codes
          1. Tips on file-management functions
      6. 7.6. Parenthesizing function arguments
        1. 7.6.1. Controlling argument-gobbling functions
      7. 7.7. Summary
        1. Directions for further study
    2. 8. Scripting techniques
      1. 8.1. Exploiting script-oriented functions
        1. 8.1.1. Defining defined
          1. Using defined for keyboard input
          2. Tips on using defined
        2. 8.1.2. Exiting with exit
        3. 8.1.3. Shifting with shift
      2. 8.2. Pre-processing arguments
        1. 8.2.1. Accommodating non-filename arguments with implicit loops
        2. 8.2.2. Filtering arguments
        3. 8.2.3. Generating arguments
      3. 8.3. Executing code conditionally with if/else
        1. 8.3.1. Employing if/else vs. and/or
        2. 8.3.2. Mixing branching techniques: The cd_report script
        3. 8.3.3. Tips on using if/else
      4. 8.4. Wrangling strings with concatenation and repetition operators
        1. 8.4.1. Enhancing the most_recent_file script
        2. 8.4.2. Using concatenation and repetition operators together
        3. 8.4.3. Tips on using the concatenation operator
      5. 8.5. Interpolating command output into source code
        1. 8.5.1. Using the tput command
          1. Highlighting trailing whitespaces with tput
        2. 8.5.2. Grepping recursively: The rgrep script
        3. 8.5.3. Tips on using command interpolation
      6. 8.6. Executing OS commands using system
        1. 8.6.1. Generating reports
        2. 8.6.2. Tips on using system
      7. 8.7. Evaluating code using eval
        1. 8.7.1. Using a Perl shell: The psh script
        2. 8.7.2. Appreciating a multi-faceted Perl grepper: The preg script
          1. Tips on using preg
      8. 8.8. Summary
        1. Directions for further study
    3. 9. List variables
      1. 9.1. Using array variables
        1. 9.1.1. Initializing arrays with piecemeal assignments and push
        2. 9.1.2. Understanding advanced array indexing
        3. 9.1.3. Extracting fields in a friendlier fashion
        4. 9.1.4. Telling fortunes: The fcookie script
          1. fcookie2: The sequel
        5. 9.1.5. Tips on using arrays
      2. 9.2. Using hash variables
        1. 9.2.1. Initializing hashes
          1. Tips on initializing hashes
        2. 9.2.2. Understanding advanced hash indexing
        3. 9.2.3. Understanding the built-in %ENV hash
        4. 9.2.4. Printing hashes
        5. 9.2.5. Using %ENV in place of switches
        6. 9.2.6. Obtaining uniqueness with hashes
          1. Rendering arguments unique
          2. Rendering inputs unique
        7. 9.2.7. Employing a hash as a simple database: The user_lookup script
        8. 9.2.8. Counting word frequencies in web pages: The count_words script
      3. 9.3. Comparing list generators in the Shell and Perl
        1. 9.3.1. Filename generation/globbing
        2. 9.3.2. Command substitution/interpolation
        3. 9.3.3. Variable substitution/interpolation
          1. Conclusion
      4. 9.4. Summary
        1. Directions for further study
    4. 10. Looping facilities
      1. 10.1. Looping facilities in the Shell and Perl
      2. 10.2. Looping with while/until
        1. 10.2.1. Totaling numeric arguments
          1. Tips on using while for list processing
        2. 10.2.2. Reducing the size of an image
        3. 10.2.3. Printing key/value pairs from a hash using each
        4. 10.2.4. Understanding the implicit loop
      3. 10.3. Looping with do while/until
        1. 10.3.1. Prompting for input
      4. 10.4. Looping with foreach
        1. 10.4.1. Unlinking files: the rm_files script
        2. 10.4.2. Reading a line at a time
        3. 10.4.3. Printing a hash
        4. 10.4.4. Demystifying acronyms: The expand_acronyms script
        5. 10.4.5. Reducing image sizes: The compress_image2 script
      5. 10.5. Looping with for
        1. 10.5.1. Exploiting for’s support for indexing: the raffle script
      6. 10.6. Using loop-control directives
        1. 10.6.1. Nesting loops within loops
        2. 10.6.2. Enabling loop-control directives in bottom-tested loops
        3. 10.6.3. Prompting for input
        4. 10.6.4. Enhancing loops with continue blocks: the confirmation script
      7. 10.7. The CPAN’s select loop for Perl
        1. 10.7.1. Avoiding the re-invention of the “choose-from-a-menu” wheel
        2. 10.7.2. Monitoring user activity: the show_user script
        3. 10.7.3. Browsing man pages: the perlman script
      8. 10.8. Summary
        1. Directions for further study
    5. 11. Subroutines and variable scoping
      1. 11.1. Compartmentalizing code with subroutines
        1. 11.1.1. Defining and using subroutines
        2. 11.1.2. Understanding use strict
          1. Strictifying a script
      2. 11.2. Common problems with variables
        1. 11.2.1. Clobbering variables: The phone_home script
        2. 11.2.2. Masking variables: The 4letter_word script
        3. 11.2.3. Tips on avoiding problems with variables
      3. 11.3. Controlling variable scoping
        1. 11.3.1. Declaring variables with my
        2. 11.3.2. Declaring variables with our
        3. 11.3.3. Declaring variables with local
        4. 11.3.4. Introducing the Variable Scoping Guidelines
          1. Guidelines for simple programs
      4. 11.4. Variable Scoping Guidelines for complex programs
        1. 11.4.1. Enable use strict
        2. 11.4.2. Declare user-defined variables and define their scopes
          1. Use the my declaration on non-switch variables
          2. Don’t let variables leak into subroutines
          3. Use the our declaration on switch variables and variables exported by modules
        3. 11.4.3. Pass data to subroutines using arguments
        4. 11.4.4. Localize temporary changes to built-in variables with local
        5. 11.4.5. Employ user-defined loop variables
        6. 11.4.6. Applying the Guidelines: the phone_home2 script
          1. Tips on using Variable Scoping Guidelines
      5. 11.5. Reusing a subroutine
      6. 11.6. Summary
        1. Directions for further study
    6. 12. Modules and the CPAN
      1. 12.1. Creating modules
        1. 12.1.1. Using the Simple Module Template
          1. Starting the new module
          2. Specifying exported components
          3. Defining subroutines
          4. Concluding the module with a “happy ending”
        2. 12.1.2. Creating a module: Center.pm
          1. Using carp in production, warn during development
          2. Setting imported variables rather than passing arguments
        3. 12.1.3. Testing a new module
          1. Testing Center with the center3 script
          2. Specifying the module’s location: The PERL5LIB variable
      2. 12.2. Managing modules
        1. 12.2.1. Identifying the modules that you want
          1. Searching for a module that calculates UPS shipping charges
        2. 12.2.2. Determining whether you have a certain module
        3. 12.2.3. Installing modules from the CPAN
          1. Configuring CPAN
          2. Installing modules as the root user
          3. Installing modules as a non-root user
      3. 12.3. Using modules
        1. 12.3.1. Business::UPS—the ups_shipping_price script
        2. 12.3.2. LWP::Simple—the check_links script
        3. 12.3.3. Shell::POSIX::Select—the menu_ls script
        4. 12.3.4. File::Find—the check_symlinks script
        5. 12.3.5. CGI—the survey.cgi script
          1. General comments on survey.cgi
          2. Using CGI functions in survey.cgi
          3. Understanding the operational modes of survey.cgi
          4. Constructing the HTML documents
          5. Output from survey.cgi
          6. The CGI cheatsheets
        6. 12.3.6. Tips on using Object-Oriented modules
      4. 12.4. Summary
        1. Directions for further study
  10. Epilogue
    1. Welcome to Perlistan!
  11. A. Perl special variables cheatsheet
  12. B. Guidelines for parenthesizing code
  13. Glossary
  14. How to use this index

Product information

  • Title: Minimal Perl
  • Author(s): Tim Maher
  • Release date: October 2006
  • Publisher(s): Manning Publications
  • ISBN: 9781932394504