Cover image for Advanced Perl Programming

Book description

So you've learned Perl, but you're getting frustrated. Perhaps you've taken on a larger project than the ones you're used to. Or you want to add a user interface or a networking component. Or you need to do more complicated error trapping. Whether your knowledge of Perl is casual or deep, this book will make you a more accomplished programmer. Here you can learn the complex techniques for production-ready Perl programs. This book explains methods for manipulating data and objects that may have looked like magic before. Furthermore, it sets Perl in the context of a larger environment, giving you the background you need for dealing with networks, databases, and GUIs. The discussion of internals helps you program more efficiently and embed Perl within C or C within Perl. Major topics covered include:

  • Practical use of packages and classes (object-oriented programming)

  • Complex data structures

  • Persistence (e.g., using a database)

  • Networking

  • Graphical interfaces, using the Tk toolkit

  • Interaction with C language functions

  • Embedding and extending the Perl interpreter

In addition, the book patiently explains all sorts of language details you've always wanted to know more about, such as the use of references, trapping errors through the eval operator, non-blocking I/O, when closures are helpful, and using ties to trigger actions when data is accessed. You will emerge from this book a better hacker, and a proud master of Perl.

Table of Contents

  1. Advanced Perl Programming
    1. Preface
      1. The Case for Scripting
      2. Why Perl?
      3. What Must I Know?
        1. Language Syntax
        2. The Perl Interpreter
        3. Technology Areas
      4. The Book’s Approach
        1. A Note to the Expert
        2. Systems View
        3. Examples
        4. FTP
        5. FTPMAIL
      5. Conventions
      6. Resources
      7. Perl Resources
      8. We’d Like to Hear from You
      9. Acknowledgments
    2. 1. Data References and Anonymous Storage
      1. Referring to Existing Variables
        1. A Reference Is Just Another Scalar
        2. Dereferencing
        3. References to Scalars
        4. References to Arrays
        5. References to Hashes
        6. Confusion About Precedence
        7. Shortcuts with the Arrow Notation
        8. No Automatic Dereferencing
      2. Using References
        1. Passing Arrays and Hashes to Subroutines
        2. Performance Efficiency
        3. References to Anonymous Storage
        4. Dereferencing Multiple Levels of Indirection
        5. A More General Rule
          1. Trojan horses
      3. Nested Data Structures
        1. Implicit Creation of Complex Structures
        2. Final Shortcut: Omit Arrows Between Subscripts
      4. Querying a Reference
      5. Symbolic References
      6. A View of the Internals
        1. Reference Counts
        2. Array/Hash References Versus Element References
      7. References in Other Languages
        1. Tcl
        2. Python
        3. C/C++
        4. Java
      8. Resources
    3. 2. Implementing Complex Data Structures
      1. User-Defined Structures
      2. Example: Matrices
        1. Hash of Hashes Representation
        2. Other Matrix Representations
      3. Professors, Students, Courses
        1. Representation
      4. Pass the Envelope
        1. Data Representation
        2. Print All Entries for a Given Year
        3. Print All Entries Sorted by Year
        4. Print a Specific Entry, Given a Year and Category
      5. Pretty-Printing
      6. Resources
    4. 3. Typeglobs and Symbol Tables
      1. Perl Variables, Symbol Table, and Scoping
        1. Lexical Variables
        2. Lexical Versus Dynamic Scoping
          1. When would you ever need to use local?
      2. Typeglobs
        1. Temporary Aliases
        2. Using Typeglob Aliases
          1. Efficient parameter passing
          2. Aliasing on command lines
          3. Friendly predefined variables with aliases
        3. Aliasing Problem: Variable Suicide
      3. Typeglobs and References
        1. Selective Aliasing
        2. Constants
        3. Naming Anonymous Subroutines
        4. References to Typeglobs
      4. Filehandles, Directory Handles, and Formats
        1. I/O Redirection
        2. Passing Filehandles to Subroutines
        3. Localizing Filehandles
        4. Strings as Handles
    5. 4. Subroutine References and Closures
      1. Subroutine References
        1. References to Named Subroutines
        2. References to Anonymous Subroutines
        3. Dereferencing Subroutine References
        4. Symbolic References
      2. Using Subroutine References
        1. Dispatch Table
        2. Signal Handlers
        3. Expression Plotting
      3. Closures
        1. Closures, Behind the Scenes
      4. Using Closures
        1. Using Closures as “Smart” Callbacks
        2. Iterators and Streams
          1. Random number generation
        3. Closures Versus Objects
      5. Comparisons to Other Languages
        1. Tcl
        2. Python
        3. C++
        4. Java
      6. Resources
    6. 5. Eval
      1. The String Form: Expression Evaluation
      2. The Block Form: Exception Handling
        1. Standard Modules
          1. Exception.pm
          2. exceptions.pl
      3. Watch Your Quotes
      4. Using Eval for Expression Evaluation
        1. Expression Evaluation in Substitutions
      5. Using Eval for Efficiency
        1. A Fast Multipattern grep
        2. Extracting Columns from a File
      6. Using Eval for Time-Outs
      7. Eval in Other Languages
        1. Tcl (Tool Command Language)
        2. Python
        3. C / C++
        4. Java
      8. Resources
    7. 6. Modules
      1. Basic Package
        1. Packages and Variables
        2. Symbolic References
      2. Packages and Files
        1. Load Path
      3. Package Initialization and Destruction
      4. Privacy
        1. Enforcing Privacy
      5. Importing Symbols
        1. How Do use and Exporter Work?
      6. Nesting Packages
      7. Autoloading
      8. Accessing the Symbol Table
      9. Language Comparisons
        1. Tcl
        2. Python
        3. C/C++
        4. Java
    8. 7. Object-Oriented Programming
      1. OO: An Introduction
      2. Objects in Perl
        1. Attributes
        2. Unique Identity
        3. Behavior
        4. The Need for Polymorphism
        5. Class Methods and Attributes
          1. Detour: The indirect notation
        6. The Need for Inheritance
          1. Overriding base classes
        7. Object Destruction
        8. Accessor Methods
      3. UNIVERSAL
        1. Searching for Methods
      4. Recap of Conventions
        1. Example
      5. Comparison with Other OO Languages
        1. Tcl
        2. Python
        3. C++ and Java
      6. Resources
    9. 8. Object Orientation: The Next Few Steps
      1. Efficient Attribute Storage
        1. ObjectTemplate: Attribute Storage Using Arrays
          1. ObjectTemplate internals overview
          2. ObjectTemplate implementation
          3. Suggested changes to ObjectTemplate
        2. Attribute Storage Using Typeglobs
      2. Delegation
      3. On Inheritance
        1. Attribute Inheritance
        2. Implementation Inheritance
        3. Interface Inheritance
        4. Using Composition Instead
      4. Resources
    10. 9. Tie
      1. Tying Scalars
        1. Example: Stopwatch
          1. tie works with anonymous values
      2. Tying Arrays
        1. TIEARRAY Example: File as an Array
      3. Tying Hashes
      4. Tying Filehandles
      5. Example: Monitoring Variables
      6. Comparisons with Other Languages
        1. Tcl
        2. Python
        3. C++
        4. Java
    11. 10. Persistence
      1. Persistence Issues
      2. Streamed Data
        1. FreezeThaw
        2. Data::Dumper
        3. Storable
      3. Record-Oriented Approach
        1. DBM
        2. MLDBM
        3. Berkeley DB
      4. Relational Databases
        1. DBI (Database Interface)
          1. Basic SQL accesses
          2. Select
          3. Query meta-data
          4. Transactions
          5. Special functions
          6. What DBI does not provide
        2. Win32::ODBC
      5. Resources
    12. 11. Implementing Object Persistence
      1. Adaptor: An Introduction
      2. Design Notes
        1. Design Goals
        2. Object Encapsulation
        3. Object-Adaptor Protocol
        4. Multivalued Attributes and Database Mapping
        5. Inheritance and Database Mapping
        6. Object Identity
        7. Object Associations
        8. Uniqueness of Objects in Memory
        9. Queries
        10. Schema Evolution
      3. Implementation
        1. Adaptor::File
          1. Storing objects
          2. Retrieving objects
          3. Query processing
        2. Adaptor::DBI
          1. Storing objects
          2. Queries
      4. Resources
    13. 12. Networking with Sockets
      1. Networking Primer
      2. Socket API and IO::Socket
        1. Receiver
        2. Sender
        3. Bidirectional Communications
      3. Handling Multiple Clients
        1. Multiple Threads of Execution
        2. Multiplexing Using select
          1. Blocking looms again
        3. Nonblocking Filehandles
      4. Real-World Servers
      5. IO Objects and Filehandles
      6. Prebuilt Client Modules
        1. Net::FTP
        2. Net::POP3
      7. Resources
    14. 13. Networking: Implementing RPC
      1. Msg: Messaging Toolkit
        1. Msg Implementation
          1. Msg: Send-side routines
          2. Msg: Receive-side routines
          3. Msg: Support for nonblocking I/O
          4. Msg: Event loop routines
      2. Remote Procedure Calls (RPC)
        1. Using RPC
        2. RPC: Implementation
      3. Resources
    15. 14. User Interfaces with Tk
      1. Introduction to GUIs, Tk, and Perl/Tk
      2. Starting with Perl/Tk
        1. GUI Forms: The Easy Way
      3. Widget Tour
        1. Widget Properties
          1. Fonts
          2. Images
          3. Colors
        2. Labels and Buttons
        3. Radiobuttons and Checkbuttons
        4. Canvas
        5. Text and Entry
          1. Inserting text at absolute positions
          2. Inserting text at logical positions
          3. Insertion with relative indexing
          4. Using tags to change properties of text ranges
          5. The entry widget
          6. Text widget and ties
        6. Listbox
        7. Frame
        8. Menus
        9. Scrollbars and Scrolling
          1. Custom scrolling
        10. Scale
        11. HList
      4. Geometry Management
        1. Packer
        2. Grid
      5. Timers
      6. Event Bindings
        1. Multiple Bindings
        2. Event Details
      7. Event Loops
      8. Resources
    16. 15. GUI Example: Tetris
      1. Introduction to Tetris
      2. Design
        1. User Interface
        2. Data Structures
      3. Implementation
    17. 16. GUI Example: Man Page Viewer
      1. man and perlman
      2. Implementation
        1. Showing Man Pages
        2. Searching for Text
        3. Screen Layout
      3. Resources
    18. 17. Template-DrivenCode Generation
      1. On Code Generation
      2. Jeeves Example
        1. Oh, and Can You Do This Too?
      3. Jeeves Overview
        1. Advantages of This Architecture
        2. Intermediate Perl Code
      4. Jeeves Implementation
        1. AST Module
        2. Template Parser
        3. Jeeves Driver
      5. Sample Specification Parser
      6. Resources
    19. 18. Extending Perl: A First Course
      1. Writing an Extension: Overview
        1. The Extension Process
        2. SWIG or XS?
      2. Example: Fractals with Perl
        1. Fractals Using SWIG
        2. Fractals Using XS
      3. SWIG Features
      4. XS Features
        1. What Does xsubpp Do?
        2. Default and Optional Parameters
        3. Modifying Parameters
        4. Special Code
        5. C++ Code
      5. Degrees of Freedom
      6. A Detour into Fractals
      7. Resources
    20. 19. Embedding Perl:The Easy Way
      1. Why Embed?
      2. Embedding Overview
      3. Examples
      4. Adding Extensions
      5. Resources
    21. 20. Perl Internals
      1. Reading the Source
      2. Architecture
        1. Perl Objects
          1. Multiple interpreters
        2. Translator
          1. Inside opcodes
          2. Compilation and code generation stages
          3. Security features
        3. Executor
      3. Perl Value Types
        1. Scalar Values
          1. Using this API
          2. Inside SVs
          3. SVs and object pointers
        2. Array Values (AV)
          1. Inside AVs
        3. Hash Values (HVs)
          1. Inside HVs
        4. Glob Values and Symbol Tables
          1. Inside glob values and symbol tables
        5. Code Values
          1. Inside CV
          2. How local and my work
          3. Closures
          4. Objects versus closures
        6. Magic Variables
      4. Stacks and Messaging Protocol
        1. Calling a Perl Subroutine
        2. The Called Side: Hand-Coding an XSUB
          1. Returning a variable list of results
          2. Ensuring that the stack is big enough
        3. Inside Other Stacks
      5. Meaty Extensions
        1. Anatomy of an XS Extension
        2. XS Typemaps: An Introduction
        3. Object Interface Using XS Typemaps
        4. Making XS Typemaps More Generic
        5. C++ Objects and XS Typemaps
        6. Memory Management Using XS
          1. Recommended memory allocation and deallocation routines
        7. SWIG Typemaps
      6. Easy Embedding API
      7. A Peek into the Future
      8. Resources
    22. A. Tk Widget Reference
      1. Button
        1. Radiobutton
        2. Checkbutton
      2. Canvas
        1. Text
      3. Entry
      4. Listbox
      5. Menus
      6. Scrollbars and Scrolling
      7. Scale
      8. HList — Hierarchical List
    23. B. Syntax Summary
      1. References
      2. Nested Data Structures
      3. Closures
      4. Modules
      5. Objects
      6. Dynamic Behavior
      7. Exception Handling
      8. Meta-Information
      9. Typeglobs
      10. Filehandles, Formats
    24. Index
    25. Colophon