Perl by Example

Book description

The World’s Easiest Perl 5 Tutorial—Updated for Today’s Applications and “Modern Perl” Best Practices

“When I look at my bookshelf, I see eleven books on Perl programming. Perl by Example, Third Edition, isn’t on the shelf; it sits on my desk, where I use it almost daily. I still think it is the best Perl book on the market for anyone—beginner or seasoned programmer—who uses Perl daily.”

Bill Maples, Enterprise Network Support, Fidelity National Information Services

Perl by Example, Fifth Edition, is the proven, easy way to master Perl 5 programming. Legendary Silicon Valley programming instructor Ellie Quigley has fully updated and focused her classic text on today’s key Perl applications, especially automation, testing, data extraction, and legacy code maintenance. She has also revised this edition to reflect “modern Perl” practices that have emerged since Perl 5.10.

Quigley illuminates every technique with focused, classroom-tested code examples. For each example, she shows you code, input, and output, and provides detailed, line-by-line explanations of how the code generates that output. And her coverage is comprehensive, from basic syntax to regular expression handling, files, references, objects, working with databases, and much more…plus appendices that contain a complete list of functions and definitions, command-line switches, special variables, and popular modules.

New in This Edition

• Modern Perl approaches to using data types, operators, conditions, subroutines, packages, modules, references, pointers, files, objects, and more

• Many new examples, covering automation, testing, and data extraction

• A tutorial on writing object-oriented Perl with the Moose object system

• An introduction to Dancer, a powerful web application framework designed to replace CGI

• Updated code examples throughout

More than 50,000 sysadmins, power users, and developers have used this book’s previous editions to become expert Perl programmers, and you can, too–even if you’re completely new to Perl. Then, once you’re an expert, you’ll routinely return to this practical guide as the best source for reliable answers, solutions, and code. A more focused, quicker read than ever, this clear and practical guide will take you from your first Perl script to advanced applications. It’s the only Perl text you’ll need.

Ellie Quigley has taught scripting in Silicon Valley for more than twenty-five years. Her Perl and shell programming classes at the University of California, Santa Cruz Extension are part of Silicon Valley lore. Her other best-selling Prentice Hall books include UNIX® Shells by Example, Fourth Edition; PHP and MySQL by Example (with Marko Gargenta); and JavaScript by Example. A major player in developing UCSC’s Silicon Valley Extension program, she has created and customized courses for pioneering firms, including Xilinx, NetApp, Yahoo, and Juniper.

Table of contents

  1. Cover
  2. About This eBook
  3. Title Page
  4. Copyright Page
  5. Praise for Ellie Quigley’s Books
  6. Contents
  7. Preface
    1. Acknowledgments
  8. Chapter 1. The Practical Extraction and Report Language
    1. 1.1 What Is Perl?
    2. 1.2 What Is an Interpreted Language?
    3. 1.3 Who Uses Perl?
      1. 1.3.1 Which Perl?
      2. 1.3.2 What Are Perl 6, Rakudo Perl, and Parrot?
    4. 1.4 Where to Get Perl
      1. 1.4.1 CPAN (cpan.org)
      2. 1.4.2 Downloads and Other Resources for Perl (perl.org)
      3. 1.4.3 ActivePerl (activestate.com)
      4. 1.4.4 What Version Do I Have?
    5. 1.5 Perl Documentation
      1. 1.5.1 Where to Find the Most Complete Documentation from Perl
      2. 1.5.2 Perl man Pages
      3. 1.5.3 Online Documentation
    6. 1.6 What You Should Know
    7. 1.7 What’s Next?
  9. Chapter 2. Perl Quick Start
    1. 2.1 Quick Start, Quick Reference
      1. 2.1.1 A Note to Programmers
      2. 2.1.2 A Note to Non-Programmers
      3. 2.1.3 Perl Syntax and Constructs
        1. Regular Expressions
        2. Passing Arguments at the Command Line
        3. References and Pointers
        4. Objects
        5. Libraries and Modules
        6. Diagnostics
    2. 2.2 Chapter Summary
    3. 2.3 What’s Next?
  10. Chapter 3. Perl Scripts
    1. 3.1 Getting Started
      1. 3.1.1 Finding a Text Editor
      2. 3.1.2 Naming Perl Scripts
      3. 3.1.3 Statements, Whitespace, and Linebreaks
      4. 3.1.4 Strings and Numbers
    2. 3.2 Filehandles
    3. 3.3 Variables (Where to Put Data)
      1. 3.3.1 What Is Context?
      2. 3.3.2 Comments
      3. 3.3.3 Perl Statements
      4. 3.3.4 Using Perl Built-in Functions
      5. 3.3.5 Script Execution
    4. 3.4 Summing It Up
      1. 3.4.1 What Kinds of Errors to Expect
    5. 3.5 Perl Switches
      1. 3.5.1 The -e Switch (Quick Test at the Command Line)
      2. 3.5.2 The -c Switch (Check Syntax)
      3. 3.5.3 The -w Switch (Warnings)
    6. 3.6 What You Should Know
    7. 3.7 What’s Next?
    8. Exercise 3: Getting with It Syntactically
  11. Chapter 4. Getting a Handle on Printing
    1. 4.1 The Special Filehandles STDOUT, STDIN, STDERR
    2. 4.2 Words
    3. 4.3 The print Function
      1. 4.3.1 Quotes Matter!
        1. Double Quotes
        2. Single Quotes
        3. Backquotes
        4. Perl’s Alternative Quotes
      2. 4.3.2 Literals (Numeric, String, and Special)
        1. Numeric Literals
        2. String Literals
        3. Special Literals
      3. 4.3.3 Printing Without Quotes—The here document
        1. here documents and CGI
    4. 4.4 Fancy Formatting with the printf Function
      1. 4.4.1 Saving Formatting with the sprintf Function
      2. 4.4.2 The No Newline say Function
    5. 4.5 What Are Pragmas?
      1. 4.5.1 The feature Pragma
      2. 4.5.2 The warnings Pragma
      3. 4.5.3 The diagnostics Pragma
      4. 4.5.4 The strict Pragma and Words
    6. 4.6 What You Should Know
    7. 4.7 What’s Next?
    8. Exercise 4: A String of Perls
  12. Chapter 5. What’s in a Name?
    1. 5.1 More About Data Types
      1. 5.1.1 Basic Data Types (Scalar, Array, Hash)
      2. 5.1.2 Package, Scope, Privacy, and Strictness
        1. Package and Scope
      3. 5.1.3 Naming Conventions
      4. 5.1.4 Assignment Statements
    2. 5.2 Scalars, Arrays, and Hashes
      1. 5.2.1 Scalar Variables
        1. Assignment
        2. The defined Function
        3. The undef Function
        4. The $_ Scalar Variable
      2. 5.2.2 Arrays
        1. Assignment
        2. Output and Input Special Variables ($, and $“)
        3. Array Size
        4. The Range Operator and Array Assignment
        5. Accessing Elements
        6. Looping Through an Array with the foreach Loop
        7. Array Copy and Slices
        8. Multidimensional Arrays—Lists of Lists
      3. 5.2.3 Hashes—Unordered Lists
        1. Assignment
        2. Accessing Hash Values
        3. Hash Slices
        4. Removing Duplicates from a List Using a Hash
      4. 5.2.4 Complex Data Structures
    3. 5.3 Array Functions
      1. 5.3.1 Adding Elements to an Array
        1. The push Function
        2. The unshift Function
      2. 5.3.2 Removing and Replacing Elements
        1. The delete Function
        2. The splice Function
        3. The pop Function
        4. The shift Function
      3. 5.3.3 Deleting Newlines
        1. The chop and chomp Functions (with Lists)
      4. 5.3.4 Searching for Elements and Index Values
        1. The grep Function
      5. 5.3.5 Creating a List from a Scalar
        1. The split Function
      6. 5.3.6 Creating a Scalar from a List
        1. The join Function
      7. 5.3.7 Transforming an Array
        1. The map Function
      8. 5.3.8 Sorting an Array
        1. The sort Function
      9. 5.3.9 Checking the Existence of an Array Index Value
        1. The exists Function
      10. 5.3.10 Reversing an Array
        1. The reverse Function
    4. 5.4 Hash (Associative Array) Functions
      1. 5.4.1 The keys Function
      2. 5.4.2 The values Function
      3. 5.4.3 The each Function
      4. 5.4.4 Removing Duplicates from a List with a Hash
      5. 5.4.5 Sorting a Hash by Keys and Values
        1. Sort Hash by Keys in Ascending Order
        2. Sort Hash by Keys in Reverse Order
        3. Sort Hash by Keys Numerically
        4. Numerically Sort a Hash by Values in Ascending Order
        5. Numerically Sort a Hash by Values in Descending Order
      6. 5.4.6 The delete Function
      7. 5.4.7 The exists Function
      8. 5.4.8 Special Hashes
        1. The %ENV Hash
        2. The %SIG Hash
        3. The %INC Hash
      9. 5.4.9 Context Revisited
    5. 5.5 What You Should Know
    6. 5.6 What’s Next?
    7. Exercise 5: The Funny Characters
  13. Chapter 6. Where’s the Operator?
    1. 6.1 About Perl Operators—More Context
      1. 6.1.1 Evaluating an Expression
    2. 6.2 Mixing Types
    3. 6.3 Precedence and Associativity
      1. 6.3.1 Assignment Operators
      2. 6.3.2 Boolean
      3. 6.3.3 Relational Operators
        1. Numeric
        2. String
      4. 6.3.4 Conditional Operators
      5. 6.3.5 Equality Operators
        1. Numeric
        2. String
      6. 6.3.6 The Smartmatch Operator
      7. 6.3.7 Logical Operators (Short-Circuit Operators)
      8. 6.3.8 Logical Word Operators
      9. 6.3.9 Arithmetic Operators and Functions
        1. Arithmetic Operators
        2. Arithmetic Functions
      10. 6.3.10 Autoincrement and Autodecrement Operators
      11. 6.3.11 Bitwise Logical Operators
        1. A Little Bit About Bits
        2. Bitwise Operators
      12. 6.3.12 Range Operator
      13. 6.3.13 Special String Operators and Functions
    4. 6.4 What You Should Know
    5. 6.5 What’s Next?
    6. Exercise 6: Operator, Operator
  14. Chapter 7. If Only, Unconditionally, Forever
    1. 7.1 Control Structures, Blocks, and Compound Statements
      1. 7.1.1 Decision Making—Conditional Constructs
        1. if and unless Statements
        2. The if Construct
        3. The if/else Construct
        4. The if/elsif/else Construct
        5. The unless Construct
    2. 7.2 Statement Modifiers and Simple Statements
      1. 7.2.1 The if Modifier
      2. 7.2.2 The unless Modifier
    3. 7.3 Repetition with Loops
      1. 7.3.1 The while Loop
      2. 7.3.2 The until Loop
      3. 7.3.3 The do/while and do/until Loops
      4. 7.3.4 The for Loop (The Three-Part Loop)
      5. 7.3.5 The foreach (for) Loop
    4. 7.4 Looping Modifiers
      1. 7.4.1 The while Modifier
      2. 7.4.2 The foreach Modifier
      3. 7.4.3 Loop Control
        1. Labels
        2. The redo and goto Statements
        3. Nested Loops and Labels
        4. The continue Statement
      4. 7.4.4 The switch Statement (given/when)
        1. The switch Feature (given/when/say)
    5. 7.5 What You Should Know
    6. 7.6 What’s Next?
    7. Exercise 7: What Are Your Conditions?
  15. Chapter 8. Regular Expressions—Pattern Matching
    1. 8.1 What Is a Regular Expression?
      1. 8.1.1 Why Do We Need Regular Expressions?
    2. 8.2 Modifiers and Simple Statements with Regular Expressions
      1. 8.2.1 Pattern Binding Operators
      2. 8.2.2 The DATA Filehandle
    3. 8.3 Regular Expression Operators
      1. 8.3.1 The m Operator and Pattern Matching
        1. The g Modifier—Global Match
        2. The i Modifier—Case Insensitivity
        3. Special Scalars for Saving Patterns
        4. The x Modifier—The Expressive Modifier
      2. 8.3.2 The s Operator and Substitution
      3. 8.3.3 The Pattern Binding Operators with Substitution
        1. Changing the Substitution Delimiters
        2. Substitution Modifiers
        3. Using the Special $& Variable in a Substitution
        4. Pattern Matching with a Real File
    4. 8.4 What You Should Know
    5. 8.5 What’s Next?
    6. Exercise 8: A Match Made in Heaven
  16. Chapter 9. Getting Control—Regular Expression Metacharacters
    1. 9.1 The RegExLib.com Library
    2. 9.2 Regular Expression Metacharacters
      1. 9.2.1 Metacharacters for Single Characters
        1. The Dot Metacharacter
        2. The s Modifier—The Dot Metacharacter and the Newline
        3. The Character Class
        4. The POSIX Bracket Expressions
      2. 9.2.2 Whitespace Metacharacters
      3. 9.2.3 Metacharacters to Repeat Pattern Matches
        1. The Greed Factor
        2. Metacharacters That Turn off Greediness
        3. Anchoring Metacharacters
        4. The m Modifier
        5. Alternation
        6. Grouping or Clustering
        7. Remembering or Capturing
        8. Turning off Greed
        9. Turning off Capturing
        10. Metacharacters That Look Ahead and Behind
      4. 9.2.4 The tr or y Operators
        1. The d Delete Option
        2. The c Complement Option
        3. The s Squeeze Option
    3. 9.3 Unicode
      1. 9.3.1 Perl and Unicode
    4. 9.4 What You Should Know
    5. 9.5 What’s Next?
    6. Exercise 9: And the Search Goes On . . .
  17. Chapter 10. Getting a Handle on Files
    1. 10.1 The User-Defined Filehandle
      1. 10.1.1 Opening Files—The open Function
      2. 10.1.2 Opening for Reading
        1. Closing the Filehandle
        2. The die Function
      3. 10.1.3 Reading from a File and Scalar Assignment
        1. The Filehandle and $_
        2. The Filehandle and a User-Defined Scalar Variable
        3. “Slurping” a File into an Array
        4. Using map to Create Fields from a File
        5. Slurping a File into a String with the read Function
      4. 10.1.4 Loading a Hash from a File
    2. 10.2 Reading from STDIN
      1. 10.2.1 Assigning Input to a Scalar Variable
      2. 10.2.2 The chop and chomp Functions
      3. 10.2.3 The read Function
      4. 10.2.4 The getc Function
      5. 10.2.5 Assigning Input to an Array
      6. 10.2.6 Assigning Input to a Hash
      7. 10.2.7 Opening for Writing
      8. 10.2.8 Win32 Binary Files
      9. 10.2.9 Opening for Appending
      10. 10.2.10 The select Function
      11. 10.2.11 File Locking with flock
      12. 10.2.12 The seek and tell Functions
        1. The seek Function
        2. The tell Function
      13. 10.2.13 Opening for Reading and Writing
      14. 10.2.14 Opening for Anonymous Pipes
        1. The Output Filter
        2. Sending the Output of a Filter to a File
        3. Input Filter
    3. 10.3 Passing Arguments
      1. 10.3.1 The @ARGV Array
      2. 10.3.2 ARGV and the Null Filehandle
      3. 10.3.3 The eof Function
      4. 10.3.4 The -i Switch—Editing Files in Place
    4. 10.4 File Testing
    5. 10.5 What You Should Know
    6. 10.6 What’s Next?
    7. Exercise 10: Getting a Handle on Things
  18. Chapter 11. How Do Subroutines Function?
    1. 11.1 Subroutines/Functions
      1. 11.1.1 Defining and Calling a Subroutine
        1. Forward Declaration
        2. Scope of Variables
    2. 11.2 Passing Arguments and the @_ Array
      1. 11.2.1 Call-by-Reference and the @_ Array
      2. 11.2.2 Assigning Values from @_
        1. Passing a Hash to a Subroutine
      3. 11.2.3 Returning a Value
      4. 11.2.4 Scoping Operators: local, my, our, and state
        1. The local Operator
        2. The my Operator
      5. 11.2.5 Using the strict Pragma (my and our)
        1. The state Feature
      6. 11.2.6 Putting It All Together
      7. 11.2.7 Prototypes
      8. 11.2.8 Context and Subroutines
        1. The wantarray Function and User-Defined Subroutines
      9. 11.2.9 Autoloading
      10. 11.2.10 BEGIN and END Blocks (Startup and Finish)
      11. 11.2.11 The subs Function
    3. 11.3 What You Should Know
    4. 11.4 What’s Next?
    5. Exercise 11: I Can’t Seem to Function Without Subroutines
  19. Chapter 12. Does This Job Require a Reference?
    1. 12.1 What Is a Reference?
      1. 12.1.1 Hard References
        1. The Backslash Operator
        2. Dereferencing the Pointer
      2. 12.1.2 References and Anonymous Variables
        1. Anonymous Arrays
        2. Anonymous Hashes
      3. 12.1.3 Nested Data Structures
        1. Using Data::Dumper
        2. Array of Lists
        3. Array of Hashes
        4. Hash of Hashes
      4. 12.1.4 More Nested Structures
      5. 12.1.5 References and Subroutines
        1. Anonymous Subroutines
        2. Subroutines and Passing by Reference
      6. 12.1.6 The ref Function
      7. 12.1.7 Symbolic References
        1. The strict Pragma
      8. 12.1.8 Typeglobs (Aliases)
        1. Filehandle References and Typeglobs
    2. 12.2 What You Should Know
    3. 12.3 What’s Next?
    4. Exercise 12: It’s Not Polite to Point!
  20. Chapter 13. Modularize It, Package It, and Send It to the Library!
    1. 13.1 Before Getting Started
      1. 13.1.1 An Analogy
      2. 13.1.2 What Is a Package?
        1. Referencing Package Variables and Subroutines from Another Package
      3. 13.1.3 What Is a Module?
      4. 13.1.4 The Symbol Table
    2. 13.2 The Standard Perl Library
      1. 13.2.1 The @INC Array
        1. Setting the PERL5LIB Environment Variable
        2. The lib Pragma
      2. 13.2.2 Packages and .pm Files
        1. The require Function
        2. The use Function (Modules and Pragmas)
        3. Using Perl to Include Your Own Library
      3. 13.2.3 Exporting and Importing
        1. The Exporter.pm Module
      4. 13.2.4 Finding Modules and Documentation from the Standard Perl Library
        1. Viewing the Contents of the Carp.pm Module
      5. 13.2.5 How to “Use” a Module from the Standard Perl Library
      6. 13.2.6 Using Perl to Create Your Own Module
        1. Creating an Import Method Without Exporter
    3. 13.3 Modules from CPAN
      1. 13.3.1 The CPAN.pm Module
        1. Retrieving a Module from CPAN with the cpan Shell
      2. 13.3.2 Using Perl Program Manager
    4. 13.4 Using Perlbrew and CPAN Minus
    5. 13.5 What You Should Know
    6. 13.6 What’s Next?
    7. Exercise 13: I Hid All My Perls in a Package
  21. Chapter 14. Bless Those Things! (Object-Oriented Perl)
    1. 14.1 The OOP Paradigm
      1. 14.1.1 What Are Objects?
      2. 14.1.2 What Is a Class?
      3. 14.1.3 Some Object-Oriented Lingo
    2. 14.2 Perl Classes, Objects, and Methods—Relating to the Real World
      1. 14.2.1 The Steps
      2. 14.2.2 A Complete Object-Oriented Perl Program
        1. A Perl Package Is a Class
        2. A Perl Class
      3. 14.2.3 Perl Objects
        1. References
        2. The Blessing
      4. 14.2.4 Methods Are Perl Subroutines
        1. Definition
        2. Types of Methods
        3. Invoking Methods
        4. Creating the Object with a Constructor
        5. Creating the Instance Methods
        6. Invoking the Methods (User Interaction)
      5. 14.2.5 Creating an Object-Oriented Module
        1. Passing Arguments to Methods
        2. Passing Parameters to Instance Methods
        3. Named Parameters and Data Checking
      6. 14.2.6 Polymorphism and Runtime Binding
      7. 14.2.7 Destructors and Garbage Collection
    3. 14.3 Anonymous Subroutines, Closures, and Privacy
      1. 14.3.1 What Is a Closure?
      2. 14.3.2 Closures and Objects
    4. 14.4 Inheritance
      1. 14.4.1 The @ISA Array and Calling Methods
      2. 14.4.2 $AUTOLOAD, sub AUTOLOAD, and UNIVERSAL
      3. 14.4.3 Derived Classes
      4. 14.4.4 Multiple Inheritance and Roles with Moose
      5. 14.4.5 Overriding a Parent Method and the SUPER Pseudo Class
    5. 14.5 Plain Old Documentation—Documenting a Module
      1. 14.5.1 pod Files
      2. 14.5.2 pod Commands
        1. Checking Your pod Commands
      3. 14.5.3 How to Use the pod Interpreters
      4. 14.5.4 Translating pod Documentation into Text
      5. 14.5.5 Translating pod Documentation into HTML
    6. 14.6 Using Objects from the Perl Library
      1. 14.6.1 An Object-Oriented Module from the Standard Perl Library
      2. 14.6.2 Using a Module with Objects from the Standard Perl Library
    7. 14.7 What You Should Know
    8. 14.8 What’s Next?
    9. Exercise 14: What’s the Object of This Lesson?
  22. Chapter 15. Perl Connects with MySQL
    1. 15.1 Introduction
    2. 15.2 What Is a Relational Database?
      1. 15.2.1 Client/Server Databases
      2. 15.2.2 Components of a Relational Database
        1. The Database Server
        2. The Database
        3. Tables
        4. Records and Fields
        5. The Database Schema
      3. 15.2.3 Talking to the Database with SQL
        1. English-like Grammar
        2. Semicolons Terminate SQL Statements
        3. Naming Conventions
        4. Reserved Words
        5. Case Sensitivity
        6. The Result Set
    3. 15.3 Getting Started with MySQL
      1. 15.3.1 Installing MySQL
      2. 15.3.2 Connecting to MySQL
        1. Editing Keys at the MySQL Console
        2. Setting a Password
      3. 15.3.3 Graphical User Tools
        1. The MySQL Query Browser
        2. The MySQL Privilege System
      4. 15.3.4 Finding the Databases
        1. Creating and Dropping a Database
      5. 15.3.5 Getting Started with Basic Commands
        1. Creating a Database with MySQL
        2. Selecting a Database with MySQL
        3. Creating a Table in the Database
        4. Data Types
        5. Adding Another Table with a Primary Key
        6. Inserting Data into Tables
        7. Selecting Data from Tables—The SELECT Command
        8. Selecting by Columns
        9. Selecting All Columns
        10. The WHERE Clause
        11. Sorting Tables
        12. Joining Tables
        13. Deleting Rows
        14. Updating Data in a Table
        15. Altering a Table
        16. Dropping a Table
        17. Dropping a Database
    4. 15.4 What Is the Perl DBI?
      1. 15.4.1 Installing the DBD Driver
        1. Without the DBD-MySQL with PPM
        2. Using PPM with Linux
        3. Installing the DBD::mysql Driver from CPAN
      2. 15.4.2 The DBI Class Methods
      3. 15.4.3 How to Use DBI
      4. 15.4.4 Connecting to and Disconnecting from the Database
        1. The connect() Method
        2. The disconnect() Method
      5. 15.4.5 Preparing a Statement Handle and Fetching Results
        1. Select, Execute, and Dump the Results
        2. Select, Execute, and Fetch a Row As an Array
        3. Select, Execute, and Fetch a Row As a Hash
      6. 15.4.6 Getting Error Messages
        1. Automatic Error Handling
        2. Manual Error Handling
        3. Binding Columns and Fetching Values
      7. 15.4.7 The ? Placeholder and Parameter Binding
        1. Binding Parameters in the execute Statement
        2. Binding Parameters and the bind_param() Method
      8. 15.4.8 Handling Quotes
      9. 15.4.9 Cached Queries
    5. 15.5 Statements That Don’t Return Anything
      1. 15.5.1 The do() Method
        1. Adding Entries
        2. Deleting Entries
        3. Updating Entries
    6. 15.6 Transactions
      1. 15.6.1 Commit and Rollback
      2. 15.6.2 Perl DBI, the Web, and the Dancer Framework
    7. 15.7 What’s Left?
    8. 15.8 What You Should Know
    9. 15.9 What’s Next?
    10. Exercise 15: Practicing Queries and Using DBI
  23. Chapter 16. Interfacing with the System
    1. 16.1 System Calls
      1. 16.1.1 Directories and Files
        1. Backslash Issues
        2. The File::Spec Module
      2. 16.1.2 Directory and File Attributes
        1. UNIX
        2. Windows
      3. 16.1.3 Finding Directories and Files
      4. 16.1.4 Creating a Directory—The mkdir Function
        1. UNIX
        2. Windows
      5. 16.1.5 Removing a Directory—The rmdir Function
      6. 16.1.6 Changing Directories—The chdir Function
      7. 16.1.7 Accessing a Directory via the Directory Filehandle
        1. The opendir Function
        2. The readdir Function
        3. The closedir Function
        4. The telldir Function
        5. The rewinddir Function
        6. The seekdir Function
      8. 16.1.8 Permissions and Ownership
        1. UNIX
        2. Windows
        3. The chmod Function (UNIX)
        4. The chmod Function (Windows)
        5. The chown Function (UNIX)
        6. The umask Function (UNIX)
      9. 16.1.9 Hard and Soft Links
        1. UNIX
        2. Windows
        3. The link and unlink Functions (UNIX)
        4. The symlink and readlink Functions (UNIX)
      10. 16.1.10 Renaming Files
        1. The rename Function (UNIX and Windows)
      11. 16.1.11 Changing Access and Modification Times
        1. The utime Function
      12. 16.1.12 File Statistics
        1. The stat and lstat Functions
      13. 16.1.13 Packing and Unpacking Data
    2. 16.2 Processes
      1. 16.2.1 UNIX Processes
      2. 16.2.2 Win32 Processes
      3. 16.2.3 The Environment (UNIX and Windows)
      4. 16.2.4 Processes and Filehandles
        1. Login Information—The getlogin Function
        2. Special Process Variables (pid, uid, euid, gid, egid)
        3. The Parent Process ID—The getppid Function and the $$ Variable
        4. The Process Group ID—The pgrp Function
      5. 16.2.5 Process Priorities and Niceness
        1. The getpriority Function
        2. The setpriority Function (nice)
      6. 16.2.6 Password Information
        1. UNIX
        2. Windows
        3. Getting a Password Entry (UNIX)—The getpwent Function
        4. Getting a Password Entry by Username—The getpwnam Function
        5. Getting a Password Entry by uid—The getpwuid Function
      7. 16.2.7 Time and Processes
        1. The Time::Piece Module
        2. The times Function
        3. The time Function (UNIX and Windows)
        4. The gmtime Function
        5. The localtime Function
      8. 16.2.8 Process Creation UNIX
        1. The fork Function
        2. The exec Function
        3. The wait and waitpid Functions
        4. The exit Function
      9. 16.2.9 Process Creation Win32
        1. The start Command
        2. The Win32::Spawn Function
        3. The Win32::Process Module
    3. 16.3 Other Ways to Interface with the Operating System
      1. 16.3.1 The syscall Function and the h2ph Script
      2. 16.3.2 Command Substitution—The Backquotes
      3. 16.3.3 The Shell.pm Module
      4. 16.3.4 The system Function
      5. 16.3.5 Globbing (Filename Expansion and Wildcards)
        1. The glob Function
    4. 16.4 Error Handling
      1. 16.4.1 The Carp Module
        1. The die Function
        2. The warn Function
      2. 16.4.2 The eval Function
    5. 16.5 Signals and the %SIG Hash
      1. 16.5.1 Catching Signals
      2. 16.5.2 Sending Signals to Processes
        1. The kill Function
        2. The alarm Function
        3. The sleep Function
      3. 16.5.3 Attention, Windows Users!
    6. 16.6 What You Should Know
    7. Exercise 16: Interfacing with the System
  24. Appendix A. Perl Built-ins, Pragmas, Modules, and the Debugger
    1. A.1 Perl Functions
    2. A.2 Special Variables
    3. A.3 Perl Pragmas
    4. A.4 Perl Modules
    5. A.5 Command-Line Switches
    6. A.6 Debugger
      1. A.6.1 Getting Information About the Debugger
      2. A.6.2 The Perl Debugger
      3. A.6.3 Entering and Exiting the Debugger
      4. A.6.4 Debugger Commands
  25. Appendix B. SQL Language Tutorial
    1. B.1 What Is SQL?
      1. B.1.1 Standarizing SQL
      2. B.1.2 Executing SQL Statements
        1. The MySQL Query Browser
      3. B.1.3 About SQL Commands/Queries
        1. English-like Grammar
        2. Semicolons Terminate SQL Statements
        3. Naming Conventions
        4. Reserved Words
        5. Case Senstivity
        6. The Result Set
      4. B.1.4 SQL and the Database
        1. The show databases Command
        2. The USE Command
      5. B.1.5 SQL Database Tables
        1. The SHOW and DESCRIBE Commands
    2. B.2 SQL Data Manipulation Language (DML)
      1. B.2.1 The SELECT Command
        1. Select Specified Columns
        2. Select All Columns
        3. The SELECT DISTINCT Statement
        4. Limiting the Number of Lines in the Result Set with LIMIT
        5. The WHERE Clause
        6. Using Quotes
        7. Using the = and <> Operators
        8. What Is NULL?
        9. The > and < Operators
        10. The AND and OR Operators
        11. The LIKE and NOT LIKE Conditions
        12. Pattern Matching and the % Wildcard
        13. The _ Wildcard
        14. The BETWEEN Statement
        15. Sorting Results with ORDER BY
      2. B.2.2 The INSERT Command
      3. B.2.3 The UPDATE Command
      4. B.2.4 The DELETE Statement
    3. B.3 SQL Data Definition Language
      1. B.3.1 Creating the Database
      2. B.3.2 SQL Data Types
      3. B.3.3 Creating a Table
      4. B.3.4 Creating a Key
        1. Primary Keys
        2. Foreign Keys
      5. B.3.5 Relations
        1. Two Tables with a Common Key
        2. Using a Fully Qualified Name and a Dot to Join the Tables
        3. Aliases
      6. B.3.6 Altering a Table
      7. B.3.7 Dropping a Table
      8. B.3.8 Dropping a Database
    4. B.4 SQL Functions
      1. B.4.1 Numeric Functions
        1. Using GROUP BY
      2. B.4.2 String Functions
      3. B.4.3 Date and Time Functions
        1. Formatting the Date and Time
        2. The MySQL EXTRACT Command
    5. B.5 Appendix Summary
    6. B.6 What You Should Know
    7. Exercise B: Do You Speak My Language?
  26. Appendix C. Introduction to Moose (A Postmodern Object System for Perl 5)
    1. C.1 Getting Started
    2. C.2 The Constructor
    3. C.3 The Attributes
      1. C.3.1 The has Function
      2. C.3.2 Before and After Moose Examples
      3. C.3.3 Moose Types
      4. C.3.4 Example Using Moose and Extensions
      5. C.3.5 Example Using Inheritance with Moose
    4. C.4 What About Moo?
    5. C.5 Appendix Summary
    6. C.6 References
  27. Appendix D. Perlbrew, CPAN, and cpanm
    1. D.1 CPAN and @INC
      1. D.1.1 Finding Modules
      2. D.1.2 Using Modules
        1. I Already Have It!
      3. D.1.3 Package Manager
      4. D.1.4 Manually: CPAN
        1. local::lib
    2. D.2 cpanm
    3. D.3 Perlbrew
    4. D.4 Caveats: C Dependencies
    5. D.5 Windows
  28. Appendix E. Dancing with Perl
    1. E.1 A New Dancer App
      1. E.1.1 Verbs
      2. E.1.2 Templating
      3. E.1.3 Parameters
      4. E.1.4 POST
    2. Exercise E: May I Have This Dance?
  29. Index
  30. Code Snippets

Product information

  • Title: Perl by Example
  • Author(s): Ellie Quigley
  • Release date: December 2014
  • Publisher(s): Addison-Wesley Professional
  • ISBN: 9780133593068