You are previewing Learning Perl, Fourth Edition.
O'Reilly logo
Learning Perl, Fourth Edition

Book Description

Learning Perl, better known as "the Llama book", starts the programmer on the way to mastery. Written by three prominent members of the Perl community who each have several years of experience teaching Perl around the world, this edition has been updated to account for all the recent changes to the language up to Perl 5.8. Perl is the language for people who want to get work done. It started as a tool for Unix system administrators who needed something powerful for small tasks. Since then, Perl has blossomed into a full-featured programming language used for web programming, database manipulation, XML processing, and system administration--on practically all platforms--while remaining the favorite tool for the small daily tasks it was designed for. You might start using Perl because you need it, but you'll continue to use it because you love it. Informed by their years of success at teaching Perl as consultants, the authors have re-engineered the Llama to better match the pace and scope appropriate for readers getting started with Perl, while retaining the detailed discussion, thorough examples, and eclectic wit for which the Llama is famous. The book includes new exercises and solutions so you can practice what you've learned while it's still fresh in your mind. Here are just some of the topics covered:

  • Perl variable types

  • subroutines

  • file operations

  • regular expressions

  • text processing

  • strings and sorting

  • process management

  • using third party modules

If you ask Perl programmers today what book they relied on most when they were learning Perl, you'll find that an overwhelming majority will point to the Llama. With good reason. Other books may teach you to program in Perl, but this book will turn you into a Perl programmer.

Table of Contents

  1. Learning Perl, 4th Edition
    1. Preface
      1. History of This Book
      2. Typographical Conventions
      3. Using Code Examples
      4. How to Contact Us
      5. Safari Enabled
      6. Acknowledgments
        1. From Randal
        2. From Tom
        3. From brian
    2. 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 Mean at the Start of the Exercise?
        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 Is Happening with Perl Now?
        6. What Is Perl 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
    3. 2. Scalar Data
      1. Numbers
        1. All Numbers Have the Same Format Internally
        2. Floating-Point Literals
        3. Integer Literals
        4. Non-Decimal 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
    4. 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
    5. 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. Non-Scalar Return Values
      11. Exercises
    6. 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. Exercises
    7. 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. Exercises
    8. 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
    9. 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. 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
    10. 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. Non-Greedy Quantifiers
        2. Matching Multiline Text
        3. Updating Many Files
        4. In-Place Editing from the Command Line
        5. Non-Capturing Parentheses
      6. Exercises
    11. 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. Logical Operators
        1. The Value of a Short-Circuit Operator
        2. The Ternary Operator, ?:
        3. Control Structures Using Partial-Evaluation Operators
      10. Exercise
    12. 11. File Tests
      1. File Test Operators
      2. The stat and lstat Functions
      3. The localtime Function
      4. Bitwise Operators
        1. Using Bitstrings
      5. Using the Special Underscore Filehandle
      6. Exercises
    13. 12. 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
    14. 13. 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
    15. 14. 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
    16. 15. 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
    17. 16. 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
    18. 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. Answer to Chapter 10 Exercise
      10. Answers to Chapter 11 Exercises
      11. Answers to Chapter 12 Exercises
      12. Answers to Chapter 13 Exercises
      13. Answers to Chapter 14 Exercises
      14. Answer to Chapter 15 Exercise
      15. Answer to Chapter 16 Exercise
    19. 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 and CGI_Lite 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 . . .
    20. Index
    21. Colophon