Cover image for Learning Perl Objects, References, and Modules

Book description

Perl is a versatile, powerful programming language used in a variety of disciplines, ranging from system administration to web programming to database manipulation. One slogan of Perl is that it makes easy things easy and hard things possible. This book is about making the leap from the easy things to the hard ones. Learning Perl Objects, References & Modules offers a gentle but thorough introduction to advanced programming in Perl. Written by the authors of the best-selling Learning Perl, this book picks up where that book left off. Topics include:

  • Packages and namespaces

  • References and scoping

  • Manipulating complex data structures

  • Object-oriented programming

  • Writing and using modules

  • Contributing to CPAN

Following the successful format of Learning Perl, each chapter in the book is designed to be small enough to be read in just an hour or two, ending with a series of exercises to help you practice what you've learned. To use the book, you just need to be familiar with the material in Learning Perl and have ambition to go further. Perl is a different language to different people. It is a quick scripting tool for some, and a fully-featured object-oriented language for others. It is used for everything from performing quick global replacements on text files, to crunching huge, complex sets of scientific data that take weeks to process. Perl is what you make of it. But regardless of what you use Perl for, this book helps you do it more effectively, efficiently, and elegantly. Learning Perl Objects, References & Modules is about learning to use Perl as a programming language, and not just a scripting language. This is the book that separates the Perl dabbler from the Perl programmer.

Table of Contents

  1. Learning Perl Objects, References & Modules
  2. A Note Regarding Supplemental Files
  3. Foreword
  4. Preface
    1. Structure of This Book
    2. Conventions Used in This Book
    3. Comments and Questions
    4. Acknowledgments
  5. 1. Introduction
    1. What Should You Know Already?
    2. What About All Those Footnotes?
    3. What’s with the Exercises?
    4. What if I’m a Perl Course Instructor?
  6. 2. Building Larger Programs
    1. The Cure for the Common Code
    2. Inserting Code with eval
    3. Using do
    4. Using require
    5. require and @INC
      1. Extending @INC
      2. Extending @INC with PERL5LIB
      3. Extending @INC with -I
    6. The Problem of Namespace Collisions
    7. Packages as Namespace Separators
    8. Scope of a Package Directive
    9. Packages and Lexicals
    10. Exercises
      1. Exercise 1 [30 min]
      2. Exercise 2 [10 min]
  7. 3. Introduction to References
    1. Performing the Same Task on Many Arrays
    2. Taking a Reference to an Array
    3. Dereferencing the Array Reference
    4. Dropping Those Braces
    5. Modifying the Array
    6. Nested Data Structures
    7. Simplifying Nested Element References with Arrows
    8. References to Hashes
    9. Exercises
      1. Exercise 1 [5 min]
      2. Exercise 2 [30 min]
  8. 4. References and Scoping
    1. More than One Reference to Data
    2. What if That Was the Name?
    3. Reference Counting and Nested Data Structures
    4. When Reference Counting Goes Bad
    5. Creating an Anonymous Array Directly
    6. Creating an Anonymous Hash
    7. Autovivification
    8. Autovivification and Hashes
    9. Exercises
      1. Exercise 1 [5 min]
      2. Exercise 2 [30 min]
  9. 5. Manipulating Complex Data Structures
    1. Using the Debugger to View Complex Data
    2. Viewing Complex Data with Data::Dumper
    3. Storing Complex Data with Storable
    4. The map and grep Operators
    5. Using map
    6. Applying a Bit of Indirection
    7. Selecting and Altering Complex Data
    8. Exercises
      1. Exercise 1 [20 min]
      2. Exercise 2 [5 min]
  10. 6. Subroutine References
    1. Referencing a Named Subroutine
    2. Anonymous Subroutines
    3. Callbacks
    4. Closures
    5. Returning a Subroutine from a Subroutine
    6. Closure Variables as Inputs
    7. Closure Variables as Static Local Variables
    8. Exercise
      1. Exercise [30 min]
  11. 7. Practical Reference Tricks
    1. Review of Sorting
    2. Sorting with Indices
    3. Sorting Efficiently
    4. The Schwartzian Transform
    5. Recursively Defined Data
    6. Building Recursively Defined Data
    7. Displaying Recursively Defined Data
    8. Exercises
      1. Exercise 1 [15 min]
      2. Exercise 2 [15 min]
      3. Exercise 3 [10 min]
      4. Exercise 4 [20 min]
  12. 8. Introduction to Objects
    1. If We Could Talk to the Animals...
    2. Introducing the Method Invocation Arrow
    3. The Extra Parameter of Method Invocation
    4. Calling a Second Method to Simplify Things
    5. A Few Notes About @ISA
    6. Overriding the Methods
    7. Starting the Search from a Different Place
    8. The SUPER Way of Doing Things
    9. What to Do with @_
    10. Where We Are So Far...
    11. Exercises
      1. Exercise 1 [20 min]
      2. Exercise 2 [40 min]
  13. 9. Objects with Data
    1. A Horse Is a Horse, of Course of Course—or Is It?
    2. Invoking an Instance Method
    3. Accessing the Instance Data
    4. How to Build a Horse
    5. Inheriting the Constructor
    6. Making a Method Work with Either Classes or Instances
    7. Adding Parameters to a Method
    8. More Interesting Instances
    9. A Horse of a Different Color
    10. Getting Your Deposit Back
    11. Don’t Look Inside the Box
    12. Faster Getters and Setters
    13. Getters That Double as Setters
    14. Restricting a Method to Class-Only or Instance-Only
    15. Exercise
      1. Exercise [45 min]
  14. 10. Object Destruction
    1. Nested Object Destruction
    2. Beating a Dead Horse
    3. Indirect Object Notation
    4. Additional Instance Variables in Subclasses
    5. Using Class Variables
    6. Weakening the Argument
    7. Exercise
      1. Exercise [45 min]
  15. 11. Some Advanced Object Topics
    1. UNIVERSAL Methods
    2. Testing Your Objects for Good Behavior
    3. AUTOLOAD as a Last Resort
    4. Using AUTOLOAD for Accessors
    5. Creating Getters and Setters More Easily
    6. Multiple Inheritance
    7. References to Filehandles
    8. Exercise
      1. Exercise [30 min]
  16. 12. Using Modules
    1. Sample Function-Oriented Interface: File::Basename
    2. Selecting What to Import
    3. Sample Object-Oriented Interface: File::Spec
    4. A More Typical Object-Oriented Module: Math::BigInt
    5. The Differences Between OO and Non-OO Modules
    6. What use Is Doing
    7. Setting the Path at the Right Time
    8. Importing with Exporter
    9. @EXPORT and @EXPORT_OK
    10. Exporting in a Primarily OO Module
    11. Custom Import Routines
    12. Exercise
      1. Exercise [15 min]
  17. 13. Writing a Distribution
    1. Starting with h2xs
    2. Looking at the Templates
    3. The Prototype Module Itself
    4. Embedded Documentation
    5. Controlling the Distribution with Makefile.PL
    6. Alternate Installation Locations (PREFIX=...)
    7. Trivial make test
    8. Trivial make install
    9. Trivial make dist
    10. Using the Alternate Library Location
    11. Exercise
      1. Exercise [30 min]
  18. 14. Essential Testing
    1. What the Test Harness Does
    2. Writing Tests with Test::Simple
    3. Writing Tests with Test::More
    4. Conditional Tests
    5. More Complex Tests (Multiple Test Scripts)
    6. Testing Things That Write to STDOUT and STDERR
    7. Exercise
      1. Exercise [60 min]
  19. 15. Contributing to CPAN
    1. The Comprehensive Perl Archive Network
    2. Getting Prepared
    3. Preparing Your Distribution
    4. Uploading Your Distribution
    5. Announcing the Module
    6. Testing on Multiple Platforms
    7. Consider Writing an Article or Giving a Talk
    8. Exercise
      1. Exercise
  20. A. Answers to Exercises
    1. Answers for Chapter 2
      1. Exercise 1 (Exercise 1 [30 min])
      2. Exercise 2 (Exercise 2 [10 min])
    2. Answers for Chapter 3
      1. Exercise 1 (Exercise 1 [5 min])
      2. Exercise 2 (Exercise 2 [30 min])
    3. Answers for Chapter 4
      1. Exercise 1 (Exercise 1 [5 min])
      2. Exercise 2 (Exercise 2 [30 min])
    4. Answers for Chapter 5
      1. Exercise 1 (Exercise 1 [20 min])
      2. Exercise 2 (Exercise 2 [5 min])
    5. Answer for Chapter 6
      1. Exercise (Exercise [30 min])
    6. Answers for Chapter 7
      1. Exercise 1 (Exercise 1 [15 min])
      2. Exercise 2 (Exercise 2 [15 min])
      3. Exercise 3 (Exercise 3 [10 min])
      4. Exercise 4 (Exercise 4 [20 min])
    7. Answers for Chapter 8
      1. Exercise 1 (Exercise 1 [20 min])
      2. Exercise 2 (Exercise 2 [40 min])
    8. Answer for Chapter 9
      1. Exercise (Exercise [45 min])
    9. Answer for Chapter 10
      1. Exercise (Exercise [45 min])
    10. Answer for Chapter 11
      1. Exercise (Exercise [30 min])
    11. Answer for Chapter 12
      1. Exercise (Exercise [15 min])
    12. Answers for Chapters 13-15
  21. Index
  22. About the Authors
  23. Colophon
  24. Copyright