Learning Perl, 5th Edition

Book description

Learning Perl, popularly known as "the Llama," is the book most programmers rely on to get started with Perl. The bestselling Perl tutorial since it was first published in 1993, this new fifth edition covers recent changes to the language up to Perl 5.10.

This book reflects the combined experience of its authors, who have taught Perl at Stonehenge Consulting since 1991. Years of classroom testing and experience helped shape the book's pace and scope, and this edition is packed with exercises that let you practice the concepts while you follow the text. Topics include:

  • Perl data & variable types
  • Subroutines
  • File operations
  • Regular expressions
  • String manipulation
  • Lists & sorting
  • Process management
  • Smart matching
  • Using third party modules

Perl is the language for people who want to get work done. Originally targeted to sysadmins for heavy-duty text processing, Perl is now a full-featured programming language suitable for almost any task on almost any platform-from short fixes on the command line to web applications, bioinformatics, finance, and much more. Other books may teach you to program in Perl, but this book will turn you into a Perl programmer.

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. Typographical Conventions
    2. How to Contact Us
    3. Using Code Examples
    4. Safari® Enabled
    5. History of This Book
    6. Acknowledgments
  3. 1. Introduction
    1. Questions and Answers
      1. Is This the Right Book for You?
      2. Why Are There So Many Footnotes?
      3. What About the Exercises and Their Answers?
      4. What Do Those Numbers at the Start of the Exercise Mean?
      5. What If I’m a Perl Course Instructor?
    2. What Does “Perl” Stand For?
      1. Why Did Larry Create Perl?
      2. Why Didn’t Larry Just Use Some Other Language?
      3. Is Perl Easy or Hard?
      4. How Did Perl Get to Be So Popular?
      5. What’s Happening with Perl Now?
      6. What’s Perl Really Good For?
      7. What Is Perl Not Good For?
    3. How Can I Get Perl?
      1. What Is CPAN?
      2. How Can I Get Support for Perl?
      3. Are There Any Other Kinds of Support?
      4. What If I Find a Bug in Perl?
    4. How Do I Make a Perl Program?
      1. A Simple Program
      2. What’s Inside That Program?
      3. How Do I Compile Perl?
    5. A Whirlwind Tour of Perl
    6. Exercises
  4. 2. Scalar Data
    1. Numbers
      1. All Numbers Have the Same Format Internally
      2. Floating-Point Literals
      3. Integer Literals
      4. Nondecimal Integer Literals
      5. Numeric Operators
    2. Strings
      1. Single-Quoted String Literals
      2. Double-Quoted String Literals
      3. String Operators
      4. Automatic Conversion Between Numbers and Strings
    3. Perl’s Built-in Warnings
    4. Scalar Variables
      1. Choosing Good Variable Names
      2. Scalar Assignment
      3. Binary Assignment Operators
    5. Output with print
      1. Interpolation of Scalar Variables into Strings
      2. Operator Precedence and Associativity
      3. Comparison Operators
    6. The if Control Structure
      1. Boolean Values
    7. Getting User Input
    8. The chomp Operator
    9. The while Control Structure
    10. The undef Value
    11. The defined Function
    12. Exercises
  5. 3. Lists and Arrays
    1. Accessing Elements of an Array
    2. Special Array Indices
    3. List Literals
      1. The qw Shortcut
    4. List Assignment
      1. The pop and push Operators
      2. The shift and unshift Operators
    5. Interpolating Arrays into Strings
    6. The foreach Control Structure
      1. Perl’s Favorite Default: $_
      2. The reverse Operator
      3. The sort Operator
    7. Scalar and List Context
      1. Using List-Producing Expressions in Scalar Context
      2. Using Scalar-Producing Expressions in List Context
      3. Forcing Scalar Context
    8. <STDIN> in List Context
    9. Exercises
  6. 4. Subroutines
    1. Defining a Subroutine
    2. Invoking a Subroutine
    3. Return Values
    4. Arguments
    5. Private Variables in Subroutines
    6. Variable-Length Parameter Lists
      1. A Better &max Routine
      2. Empty Parameter Lists
    7. Notes on Lexical (my) Variables
    8. The use strict Pragma
    9. The return Operator
      1. Omitting the Ampersand
    10. Nonscalar Return Values
    11. Persistent, Private Variables
    12. Exercises
  7. 5. Input and Output
    1. Input from Standard Input
    2. Input from the Diamond Operator
    3. The Invocation Arguments
    4. Output to Standard Output
    5. Formatted Output with printf
      1. Arrays and printf
    6. Filehandles
    7. Opening a Filehandle
      1. Bad Filehandles
      2. Closing a Filehandle
    8. Fatal Errors with die
      1. Warning Messages with warn
    9. Using Filehandles
      1. Changing the Default Output Filehandle
    10. Reopening a Standard Filehandle
    11. Output with say
    12. Exercises
  8. 6. Hashes
    1. What Is a Hash?
      1. Why Use a Hash?
    2. Hash Element Access
      1. The Hash As a Whole
      2. Hash Assignment
      3. The Big Arrow
    3. Hash Functions
      1. The keys and values Functions
      2. The each Function
    4. Typical Use of a Hash
      1. The exists Function
      2. The delete Function
      3. Hash Element Interpolation
    5. The %ENV hash
    6. Exercises
  9. 7. In the World of Regular Expressions
    1. What Are Regular Expressions?
    2. Using Simple Patterns
      1. About Metacharacters
      2. Simple Quantifiers
      3. Grouping in Patterns
      4. Alternatives
    3. Character Classes
      1. Character Class Shortcuts
      2. Negating the Shortcuts
    4. Exercises
  10. 8. Matching with Regular Expressions
    1. Matches with m//
    2. Option Modifiers
      1. Case-Insensitive Matching with /i
      2. Matching Any Character with /s
      3. Adding Whitespace with /x
      4. Combining Option Modifiers
      5. Other Options
    3. Anchors
      1. Word Anchors
    4. The Binding Operator, =~
    5. Interpolating into Patterns
    6. The Match Variables
      1. The Persistence of Memory
      2. Noncapturing Parentheses
      3. Named Captures
      4. The Automatic Match Variables
    7. General Quantifiers
    8. Precedence
      1. Examples of Precedence
      2. And There’s More
    9. A Pattern Test Program
    10. Exercises
  11. 9. Processing Text with Regular Expressions
    1. Substitutions with s///
      1. Global Replacements with /g
      2. Different Delimiters
      3. Option Modifiers
      4. The Binding Operator
      5. Case Shifting
    2. The split Operator
    3. The join Function
    4. m// in List Context
    5. More Powerful Regular Expressions
      1. Nongreedy Quantifiers
      2. Matching Multiple-Line Text
      3. Updating Many Files
      4. In-Place Editing from the Command Line
    6. Exercises
  12. 10. More Control Structures
    1. The unless Control Structure
      1. The else Clause with unless
    2. The until Control Structure
    3. Expression Modifiers
    4. The Naked Block Control Structure
    5. The elsif Clause
    6. Autoincrement and Autodecrement
      1. The Value of Autoincrement
    7. The for Control Structure
      1. The Secret Connection Between foreach and for
    8. Loop Controls
      1. The last Operator
      2. The next Operator
      3. The redo Operator
      4. Labeled Blocks
    9. The Ternary Operator, ?:
    10. Logical Operators
      1. The Value of a Short-Circuit Operator
      2. The defined-or Operator
      3. Control Structures Using Partial-Evaluation Operators
    11. Exercises
  13. 11. Perl Modules
    1. Finding Modules
    2. Installing Modules
    3. Using Simple Modules
      1. The File::Basename Module
      2. Using Only Some Functions from a Module
      3. The File::Spec Module
      4. CGI.pm
      5. Databases and DBI
    4. Exercise
  14. 12. File Tests
    1. File Test Operators
      1. Testing Several Attributes of the Same File
      2. Stacked File Test Operators
    2. The stat and lstat Functions
    3. The localtime Function
    4. Bitwise Operators
      1. Using Bitstrings
    5. Exercises
  15. 13. Directory Operations
    1. Moving Around the Directory Tree
    2. Globbing
    3. An Alternate Syntax for Globbing
    4. Directory Handles
    5. Recursive Directory Listing
    6. Manipulating Files and Directories
    7. Removing Files
    8. Renaming Files
    9. Links and Files
    10. Making and Removing Directories
    11. Modifying Permissions
    12. Changing Ownership
    13. Changing Timestamps
    14. Exercises
  16. 14. Strings and Sorting
    1. Finding a Substring with index
    2. Manipulating a Substring with substr
    3. Formatting Data with sprintf
      1. Using sprintf with “Money Numbers”
    4. Advanced Sorting
      1. Sorting a Hash by Value
      2. Sorting by Multiple Keys
    5. Exercises
  17. 15. Smart Matching and given-when
    1. The Smart Match Operator
    2. Smart Match Precedence
    3. The given Statement
      1. Dumb Matching
    4. when with Many Items
    5. Exercises
  18. 16. Process Management
    1. The system Function
      1. Avoiding the Shell
    2. The exec Function
    3. The Environment Variables
    4. Using Backquotes to Capture Output
      1. Using Backquotes in a List Context
    5. Processes as Filehandles
    6. Getting Down and Dirty with Fork
    7. Sending and Receiving Signals
    8. Exercises
  19. 17. Some Advanced Perl Techniques
    1. Trapping Errors with eval
    2. Picking Items from a List with grep
    3. Transforming Items from a List with map
    4. Unquoted Hash Keys
    5. Slices
      1. Array Slice
      2. Hash Slice
    6. Exercise
  20. A. Exercise Answers
    1. Answers to Chapter 2 Exercises
    2. Answers to Chapter 3 Exercises
    3. Answers to Chapter 4 Exercises
    4. Answers to Chapter 5 Exercises
    5. Answers to Chapter 6 Exercises
    6. Answers to Chapter 7 Exercises
    7. Answers to Chapter 8 Exercises
    8. Answers to Chapter 9 Exercises
    9. Answers to Chapter 10 Exercises
    10. Answer to Chapter 11 Exercise
    11. Answers to Chapter 12 Exercises
    12. Answers to Chapter 13 Exercises
    13. Answers to Chapter 14 Exercises
    14. Answers to Chapter 15 Exercises
    15. Answers to Chapter 16 Exercises
    16. Answer to Chapter 17 Exercise
  21. B. Beyond the Llama
    1. Further Documentation
    2. Regular Expressions
    3. Packages
    4. Extending Perl’s Functionality
      1. Libraries
      2. Modules
      3. Finding and Installing Modules
      4. Writing Your Own Modules
    5. Some Important Modules
      1. The CGI Modules
      2. The Cwd Module
      3. The Fatal Module
      4. The File::Basename Module
      5. The File::Copy Module
      6. The File::Spec Module
      7. The Image::Size Module
      8. The Net::SMTP Module
      9. The POSIX Module
      10. The Sys::Hostname Module
      11. The Text::Wrap Module
      12. The Time::Local Module
    6. Pragmas
      1. The constant Pragma
      2. The diagnostics Pragma
      3. The lib Pragma
      4. The strict Pragma
      5. The vars Pragma
      6. The warnings Pragma
    7. Databases
      1. Direct System Database Access
      2. Flat-file Database Access
    8. Other Operators and Functions
      1. Transliteration with tr///
      2. Here Documents
    9. Mathematics
      1. Advanced Math Functions
      2. Imaginary and Complex Numbers
      3. Large and High-Precision Numbers
    10. Lists and Arrays
      1. map and grep
      2. The splice Operator
    11. Bits and Pieces
    12. Formats
    13. Networking and IPC
      1. System V IPC
      2. Sockets
    14. Security
    15. Debugging
    16. The Common Gateway Interface (CGI)
    17. Command-Line Options
    18. Built-in Variables
    19. Syntax Extensions
    20. References
      1. Complex Data Structures
      2. Object-Oriented Programming
      3. Anonymous Subroutines and Closures
    21. Tied Variables
    22. Operator Overloading
    23. Dynamic Loading
    24. Embedding
    25. Converting Other Languages to Perl
    26. Converting find Command Lines to Perl
    27. Command-Line Options in Your Programs
    28. Embedded Documentation
    29. More Ways to Open Filehandles
    30. Locales and Unicode
    31. Threads and Forking
    32. Graphical User Interfaces (GUIs)
    33. And More…
  22. Index
  23. About the Authors
  24. Colophon
  25. Copyright

Product information

  • Title: Learning Perl, 5th Edition
  • Author(s): Randal L. Schwartz, Tom Phoenix, brian d foy
  • Release date: June 2008
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596520106