Contents

Preface

1 The Practical Extraction and Report Language

1.1 What Is Perl?

1.2 What Is an Interpreted Language?

1.3 Who Uses Perl?

1.3.1 Which Perl?

1.3.2 What Are Perl 6, Rakudo Perl, and Parrot?

1.4 Where to Get Perl

1.4.1 CPAN (cpan.org)

1.4.2 Downloads and Other Resources for Perl (perl.org)

1.4.3 ActivePerl (activestate.com)

1.4.4 What Version Do I Have?

1.5 Perl Documentation

1.5.1 Where to Find the Most Complete Documentation from Perl

1.5.2 Perl man Pages

1.5.3 Online Documentation

1.6 What You Should Know

1.7 What’s Next?

2 Perl Quick Start

2.1 Quick Start, Quick Reference

2.1.1 A Note to Programmers

2.1.2 A Note to Non-Programmers

2.1.3 Perl Syntax and Constructs

Regular Expressions

Passing Arguments at the Command Line

References and Pointers

Objects

Libraries and Modules

Diagnostics

2.2 Chapter Summary

2.3 What’s Next?

3 Perl Scripts

3.1 Getting Started

3.1.1 Finding a Text Editor

3.1.2 Naming Perl Scripts

3.1.3 Statements, Whitespace, and Linebreaks

3.1.4 Strings and Numbers

3.2 Filehandles

3.3 Variables (Where to Put Data)

3.3.1 What Is Context?

3.3.2 Comments

3.3.3 Perl Statements

3.3.4 Using Perl Built-in Functions

3.3.5 Script Execution

3.4 Summing It Up

3.4.1 What Kinds of Errors to Expect

3.5 Perl Switches

3.5.1 The -e Switch (Quick Test at the Command Line)

3.5.2 The -c Switch (Check Syntax)

3.5.3 The -w Switch (Warnings)

3.6 What You Should Know

3.7 What’s Next?

EXERCISE 3 Getting with It Syntactically

4 Getting a Handle on Printing

4.1 The Special Filehandles STDOUT, STDIN, STDERR

4.2 Words

4.3 The print Function

4.3.1 Quotes Matter!

Double Quotes

Single Quotes

Backquotes

Perl’s Alternative Quotes

4.3.2 Literals (Numeric, String, and Special)

Numeric Literals

String Literals

Special Literals

4.3.3 Printing Without Quotes—The here document

here documents and CGI

4.4 Fancy Formatting with the printf Function

4.4.1 Saving Formatting with the sprintf Function

4.4.2 The No Newline say Function

4.5 What Are Pragmas?

4.5.1 The feature Pragma

4.5.2 The warnings Pragma

4.5.3 The diagnostics Pragma

4.5.4 The strict Pragma and Words

4.6 What You Should Know

4.7 What’s Next?

EXERCISE 4 A String of Perls

5 What’s In a Name?

5.1 More About Data Types

5.1.1 Basic Data Types (Scalar, Array, Hash)

5.1.2 Package, Scope, Privacy, and Strictness

Package and Scope

5.1.3 Naming Conventions

5.1.4 Assignment Statements

5.2 Scalars, Arrays, and Hashes

5.2.1 Scalar Variables

Assignment

The defined Function

The undef Function

The $_ Scalar Variable

5.2.2 Arrays

Assignment

Output and Input Special Variables ($, and $“)

Array Size

The Range Operator and Array Assignment

Accessing Elements

Looping Through an Array with the foreach Loop

Array Copy and Slices

Multidimensional Arrays—Lists of Lists

5.2.3 Hashes—Unordered Lists

Assignment

Accessing Hash Values

Hash Slices

Removing Duplicates from a List Using a Hash

5.2.4 Complex Data Structures

5.3 Array Functions

5.3.1 Adding Elements to an Array

The push Function

The unshift Function

5.3.2 Removing and Replacing Elements

The delete Function

The splice Function

The pop Function

The shift Function

5.3.3 Deleting Newlines

The chop and chomp Functions (with Lists)

5.3.4 Searching for Elements and Index Values

The grep Function

5.3.5 Creating a List from a Scalar

The split Function

5.3.6 Creating a Scalar from a List

The join Function

5.3.7 Transforming an Array

The map Function

5.3.8 Sorting an Array

The sort Function

5.3.9 Checking the Existence of an Array Index Value

The exists Function

5.3.10 Reversing an Array

The reverse Function

5.4 Hash (Associative Array) Functions

5.4.1 The keys Function

5.4.2 The values Function

5.4.3 The each Function

5.4.4 Removing Duplicates from a List with a Hash

5.4.5 Sorting a Hash by Keys and Values

Sort Hash by Keys in Ascending Order

Sort Hash by Keys in Reverse Order

Sort Hash by Keys Numerically

Numerically Sort a Hash by Values in Ascending Order

Numerically Sort a Hash by Values in Descending Order

5.4.6 The delete Function

5.4.7 The exists Function

5.4.8 Special Hashes

The %ENV Hash

The %SIG Hash

The %INC Hash

5.4.9 Context Revisited

5.5 What You Should Know

5.6 What’s Next?

EXERCISE 5 The Funny Characters

6 Where’s the Operator?

6.1 About Perl Operators—More Context

6.1.1 Evaluating an Expression

6.2 Mixing Types

6.3 Precedence and Associativity

6.3.1 Assignment Operators

6.3.2 Boolean

6.3.3 Relational Operators

Numeric

String

6.3.4 Conditional Operators

6.3.5 Equality Operators

Numeric

String

6.3.6 The Smartmatch Operator

6.3.7 Logical Operators (Short-Circuit Operators)

6.3.8 Logical Word Operators

6.3.9 Arithmetic Operators and Functions

Arithmetic Operators

Arithmetic Functions

6.3.10 Autoincrement and Autodecrement Operators

6.3.11 Bitwise Logical Operators

A Little Bit About Bits

Bitwise Operators

6.3.12 Range Operator

6.3.13 Special String Operators and Functions

6.4 What You Should Know

6.5 What’s Next?

EXERCISE 6 Operator, Operator

7 If Only, Unconditionally, Forever

7.1 Control Structures, Blocks, and Compound Statements

7.1.1 Decision Making—Conditional Constructs

if and unless Statements

The if Construct

The if/else Construct

The if/elsif/else Construct

The unless Construct

7.2 Statement Modifiers and Simple Statements

7.2.1 The if Modifier

7.2.2 The unless Modifier

7.3 Repetition with Loops

7.3.1 The while Loop

7.3.2 The until Loop

7.3.3 The do/while and do/until Loops

7.3.4 The for Loop (The Three-Part Loop)

7.3.5 The foreach (for) Loop

7.4 Looping Modifiers

7.4.1 The while Modifier

7.4.2 The foreach Modifier

7.4.3 Loop Control

Labels

The redo and goto Statements

Nested Loops and Labels

The continue Statement

7.4.4 The switch Statement (given/when)

The switch Feature (given/when/say)

7.5 What You Should Know

7.6 What’s Next?

EXERCISE 7 What Are Your Conditions?

8 Regular Expressions—Pattern Matching

8.1 What Is a Regular Expression?

8.1.1 Why Do We Need Regular Expressions?

8.2 Modifiers and Simple Statements with Regular Expressions

8.2.1 Pattern Binding Operators

8.2.2 The DATA Filehandle

8.3 Regular Expression Operators

8.3.1 The m Operator and Pattern Matching

The g Modifier—Global Match

The i Modifier—Case Insensitivity

Special Scalars for Saving Patterns

The x Modifier—The Expressive Modifier

8.3.2 The s Operator and Substitution

8.3.3 The Pattern Binding Operators with Substitution

Changing the Substitution Delimiters

Substitution Modifiers

Using the Special $& Variable in a Substitution

Pattern Matching with a Real File

8.4 What You Should Know

8.5 What’s Next?

EXERCISE 8 A Match Made in Heaven

9 Getting Control—Regular Expression Metacharacters

9.1 The RegExLib.com Library

9.2 Regular Expression Metacharacters

9.2.1 Metacharacters for Single Characters

The Dot Metacharacter

The s Modifier—The Dot Metacharacter and the Newline

The Character Class

The POSIX Bracket Expressions

9.2.2 Whitespace Metacharacters

9.2.3 Metacharacters to Repeat Pattern Matches

The Greed Factor

Metacharacters That Turn off Greediness

Anchoring Metacharacters

The m Modifier

Alternation

Grouping or Clustering

Remembering or Capturing

Turning off Greed

Turning off Capturing

Metacharacters That Look Ahead and Behind

9.2.4 The tr or y Operators

The d Delete Option

The c Complement Option

The s Squeeze Option

9.3 Unicode

9.3.1 Perl and Unicode

9.4 What You Should Know

9.5 What’s Next?

EXERCISE 9 And the Search Goes On ...

10 Getting a Handle on Files

10.1 The User-Defined Filehandle

10.1.1 Opening Files—The open Function

10.1.2 Opening for Reading

Closing the Filehandle

The die Function

10.1.3 Reading from a File and Scalar Assignment

The Filehandle and $_

The Filehandle and a User-Defined Scalar Variable

“Slurping” a File into an Array

Using map to Create Fields from a File

Slurping a File into a String with the read Function

10.1.4 Loading a Hash from a File

10.2 Reading from STDIN

10.2.1 Assigning Input to a Scalar Variable

10.2.2 The chop and chomp Functions

10.2.3 The read Function

10.2.4 The getc Function

10.2.5 Assigning Input to an Array

10.2.6 Assigning Input to a Hash

10.2.7 Opening for Writing

10.2.8 Win32 Binary Files

10.2.9 Opening for Appending

10.2.10 The select Function

10.2.11 File Locking with flock

10.2.12 The seek and tell Functions

The seek Function

The tell Function

10.2.13 Opening for Reading and Writing

10.2.14 Opening for Anonymous Pipes

The Output Filter

Sending the Output of a Filter to a File

Input Filter

10.3 Passing Arguments

10.3.1 The @ARGV Array

10.3.2 ARGV and the Null Filehandle

10.3.3 The eof Function

10.3.4 The -i Switch—Editing Files in Place

10.4 File Testing

10.5 What You Should Know

10.6 What’s Next?

EXERCISE 10 Getting a Handle on Things

11 How Do Subroutines Function?

11.1 Subroutines/Functions

11.1.1 Defining and Calling a Subroutine

Forward Declaration

Scope of Variables

11.2 Passing Arguments and the @_ Array

11.2.1 Call-by-Reference and the @_ Array

11.2.2 Assigning Values from @_

Passing a Hash to a Subroutine

11.2.3 Returning a Value

11.2.4 Scoping Operators: local, my, our, and state

The local Operator

The my Operator

11.2.5 Using the strict Pragma (my and our)

The state Feature

11.2.6 Putting It All Together

11.2.7 Prototypes

11.2.8 Context and Subroutines

The wantarray Function and User-Defined Subroutines

11.2.9 Autoloading

11.2.10 BEGIN and END Blocks (Startup and Finish)

11.2.11 The subs Function

11.3 What You Should Know

11.4 What’s Next?

EXERCISE 11 I Can’t Seem to Function Without Subroutines

12 Does This Job Require a Reference?

12.1 What Is a Reference?

12.1.1 Hard References

The Backslash Operator

Dereferencing the Pointer

12.1.2 References and Anonymous Variables

Anonymous Arrays

Anonymous Hashes

12.1.3 Nested Data Structures

Using Data::Dumper

Array of Lists

Array of Hashes

Hash of Hashes

12.1.4 More Nested Structures

12.1.5 References and Subroutines

Anonymous Subroutines

Subroutines and Passing by Reference

12.1.6 The ref Function

12.1.7 Symbolic References

The strict Pragma

12.1.8 Typeglobs (Aliases)

Filehandle References and Typeglobs

12.2 What You Should Know

12.3 What’s Next?

EXERCISE 12 It’s Not Polite to Point!

13 Modularize It, Package It, and Send It to the Library!

13.1 Before Getting Started

13.1.1 An Analogy

13.1.2 What Is a Package?

Referencing Package Variables and Subroutines from Another Package

13.1.3 What Is a Module?

13.1.4 The Symbol Table

13.2 The Standard Perl Library

13.2.1 The @INC Array

Setting the PERL5LIB Environment Variable

The lib Pragma

13.2.2 Packages and .pm Files

The require Function

The use Function (Modules and Pragmas)

Using Perl to Include Your Own Library

13.2.3 Exporting and Importing

The Exporter.pm Module

13.2.4 Finding Modules and Documentation from the Standard Perl Library

Viewing the Contents of the Carp.pm Module

13.2.5 How to “Use” a Module from the Standard Perl Library

13.2.6 Using Perl to Create Your Own Module

Creating an Import Method Without Exporter

13.3 Modules from CPAN

13.3.1 The CPAN.pm Module

Retrieving a Module from CPAN with the cpan Shell

13.3.2 Using Perl Program Manager

13.4 Using Perlbrew and CPAN Minus

13.5 What You Should Know

13.6 What’s Next?

EXERCISE 13 I Hid All My Perls in a Package

14 Bless Those Things! (Object-Oriented Perl)

14.1 The OOP Paradigm

14.1.1 What Are Objects?

14.1.2 What Is a Class?

14.1.3 Some Object-Oriented Lingo

14.2 Perl Classes, Objects, and Methods—Relating to the Real World

14.2.1 The Steps

14.2.2 A Complete Object-Oriented Perl Program

A Perl Package Is a Class

A Perl Class

14.2.3 Perl Objects

References

The Blessing

14.2.4 Methods Are Perl Subroutines

Definition

Types of Methods

Invoking Methods

Creating the Object with a Constructor

Creating the Instance Methods

Invoking the Methods (User Interaction)

14.2.5 Creating an Object-Oriented Module

Passing Arguments to Methods

Passing Parameters to Instance Methods

Named Parameters and Data Checking

14.2.6 Polymorphism and Runtime Binding

14.2.7 Destructors and Garbage Collection

14.3 Anonymous Subroutines, Closures, and Privacy

14.3.1 What Is a Closure?

14.3.2 Closures and Objects

14.4 Inheritance

14.4.1 The @ISA Array and Calling Methods

14.4.2 $AUTOLOAD, sub AUTOLOAD, and UNIVERSAL

14.4.3 Derived Classes

14.4.4 Multiple Inheritance and Roles with Moose

14.4.5 Overriding a Parent Method and the SUPER Pseudo Class

14.5 Plain Old Documentation—Documenting a Module

14.5.1 pod Files

14.5.2 pod Commands

Checking Your pod Commands

14.5.3 How to Use the pod Interpreters

14.5.4 Translating pod Documentation into Text

14.5.5 Translating pod Documentation into HTML

14.6 Using Objects from the Perl Library

14.6.1 An Object-Oriented Module from the Standard Perl Library

14.6.2 Using a Module with Objects from the Standard Perl Library

14.7 What You Should Know

14.8 What’s Next?

EXERCISE 14 What’s the Object of This Lesson?

15 Perl Connects with MySQL

15.1 Introduction

15.2 What Is a Relational Database?

15.2.1 Client/Server Databases

15.2.2 Components of a Relational Database

The Database Server

The Database

Tables

Records and Fields

The Database Schema

15.2.3 Talking to the Database with SQL

English-like Grammar

Semicolons Terminate SQL Statements

Naming Conventions

Reserved Words

Case Sensitivity

The Result Set

15.3 Getting Started with MySQL

15.3.1 Installing MySQL

15.3.2 Connecting to MySQL

Editing Keys at the MySQL Console

Setting a Password

15.3.3 Graphical User Tools

The MySQL Query Browser

The MySQL Privilege System

15.3.4 Finding the Databases

Creating and Dropping a Database

15.3.5 Getting Started with Basic Commands

Creating a Database with MySQL

Selecting a Database with MySQL

Creating a Table in the Database

Data Types

Adding Another Table with a Primary Key

Inserting Data into Tables

Selecting Data from Tables—The SELECT Command

Selecting by Columns

Selecting All Columns

The WHERE Clause

Sorting Tables

Joining Tables

Deleting Rows

Updating Data in a Table

Altering a Table

Dropping a Table

Dropping a Database

15.4 What Is the Perl DBI?

15.4.1 Installing the DBD Driver

Without the DBD-MySQL with PPM

Using PPM with Linux

Installing the DBD::mysql Driver from CPAN

15.4.2 The DBI Class Methods

15.4.3 How to Use DBI

15.4.4 Connecting to and Disconnecting from the Database

The connect() Method

The disconnect() Method

15.4.5 Preparing a Statement Handle and Fetching Results

Select, Execute, and Dump the Results

Select, Execute, and Fetch a Row As an Array

Select, Execute, and Fetch a Row As a Hash

15.4.6 Getting Error Messages

Automatic Error Handling

Manual Error Handling

Binding Columns and Fetching Values

15.4.7 The ? Placeholder and Parameter Binding

Binding Parameters in the execute Statement

Binding Parameters and the bind_param() Method

15.4.8 Handling Quotes

15.4.9 Cached Queries

15.5 Statements That Don’t Return Anything

15.5.1 The do() Method

Adding Entries

Deleting Entries

Updating Entries

15.6 Transactions

15.6.1 Commit and Rollback

15.6.2 Perl DBI, the Web, and the Dancer Framework

15.7 What’s Left?

15.8 What You Should Know

15.9 What’s Next?

EXERCISE 15 Practicing Queries and Using DBI

16 Interfacing with the System

16.1 System Calls

16.1.1 Directories and Files

Backslash Issues

The File::Spec Module

16.1.2 Directory and File Attributes

UNIX

Windows

16.1.3 Finding Directories and Files

16.1.4 Creating a Directory—The mkdir Function

UNIX

Windows

16.1.5 Removing a Directory—The rmdir Function

16.1.6 Changing Directories—The chdir Function

16.1.7 Accessing a Directory via the Directory Filehandle

The opendir Function

The readdir Function

The closedir Function

The telldir Function

The rewinddir Function

The seekdir Function

16.1.8 Permissions and Ownership

UNIX

Windows

The chmod Function (UNIX)

The chmod Function (Windows)

The chown Function (UNIX)

The umask Function (UNIX)

16.1.9 Hard and Soft Links

UNIX

Windows

The link and unlink Functions (UNIX)

The symlink and readlink Functions (UNIX)

16.1.10 Renaming Files

The rename Function (UNIX and Windows)

16.1.11 Changing Access and Modification Times

The utime Function

16.1.12 File Statistics

The stat and lstat Functions

16.1.13 Packing and Unpacking Data

16.2 Processes

16.2.1 UNIX Processes

16.2.2 Win32 Processes

16.2.3 The Environment (UNIX and Windows)

16.2.4 Processes and Filehandles

Login Information—The getlogin Function

Special Process Variables (pid, uid, euid, gid, egid)

The Parent Process ID—The getppid Function and the $$ Variable

The Process Group ID—The pgrp Function

16.2.5 Process Priorities and Niceness

The getpriority Function

The setpriority Function (nice)

16.2.6 Password Information

UNIX

Windows

Getting a Password Entry (UNIX)—The getpwent Function

Getting a Password Entry by Username—The getpwnam Function

Getting a Password Entry by uid—The getpwuid Function

16.2.7 Time and Processes

The Time::Piece Module

The times Function

The time Function (UNIX and Windows)

The gmtime Function

The localtime Function

16.2.8 Process Creation UNIX

The fork Function

The exec Function

The wait and waitpid Functions

The exit Function

16.2.9 Process Creation Win32

The start Command

The Win32::Spawn Function

The Win32::Process Module

16.3 Other Ways to Interface with the Operating System

16.3.1 The syscall Function and the h2ph Script

16.3.2 Command Substitution—The Backquotes

16.3.3 The Shell.pm Module

16.3.4 The system Function

16.3.5 Globbing (Filename Expansion and Wildcards)

16.4 Error Handling

16.4.1 The Carp Module

The die Function

The warn Function

16.4.2 The eval Function

16.5 Signals and the %SIG Hash

16.5.1 Catching Signals

16.5.2 Sending Signals to Processes

The kill Function

The alarm Function

The sleep Function

16.5.3 Attention, Windows Users!

16.6 What You Should Know

EXERCISE 16 Interfacing with the System

A Perl Built-ins, Pragmas, Modules, and the Debugger

A.1 Perl Functions

A.2 Special Variables

A.3 Perl Pragmas

A.4 Perl Modules

A.5 Command-Line Switches

A.6 Debugger

A.6.1 Getting Information About the Debugger

A.6.2 The Perl Debugger

A.6.3 Entering and Exiting the Debugger

A.6.4 Debugger Commands

B SQL Language Tutorial

B.1 What Is SQL?

B.1.1 Standarizing SQL

B.1.2 Executing SQL Statements

The MySQL Query Browser

B.1.3 About SQL Commands/Queries

English-like Grammar

Semicolons Terminate SQL Statements

Naming Conventions

Reserved Words

Case Senstivity

The Result Set

B.1.4 SQL and the Database

The show databases Command

The USE Command

B.1.5 SQL Database Tables

The SHOW and DESCRIBE Commands

B.2 SQL Data Manipulation Language (DML)

B.2.1 The SELECT Command

Select Specified Columns

Select All Columns

The SELECT DISTINCT Statement

Limiting the Number of Lines in the Result Set with LIMIT

The WHERE Clause

Using Quotes

Using the = and <> Operators

What Is NULL?

The > and < Operators

The AND and OR Operators

The LIKE and NOT LIKE Conditions

Pattern Matching and the % Wildcard

The _ Wildcard

The BETWEEN Statement

Sorting Results with ORDER BY

B.2.2 The INSERT Command

B.2.3 The UPDATE Command

B.2.4 The DELETE Statement

B.3 SQL Data Definition Language

B.3.1 Creating the Database

B.3.2 SQL Data Types

B.3.3 Creating a Table

B.3.4 Creating a Key

Primary Keys

Foreign Keys

B.3.5 Relations

Two Tables with a Common Key

Using a Fully Qualified Name and a Dot to Join the Tables

Aliases

B.3.6 Altering a Table

B.3.7 Dropping a Table

B.3.8 Dropping a Database

B.4 SQL Functions

B.4.1 Numeric Functions

Using GROUP BY

B.4.2 String Functions

B.4.3 Date and Time Functions

Formatting the Date and Time

The MySQL EXTRACT Command

B.5 Appendix Summary

B.6 What You Should Know

EXERCISE B Do You Speak My Language?

C Introduction to Moose (A Postmodern Object System for Perl 5)

C.1 Getting Started

C.2 The Constructor

C.3 The Attributes

C.3.1 The has Function

C.3.2 Before and After Moose Examples

C.3.3 Moose Types

C.3.4 Example Using Moose and Extensions

C.3.5 Example Using Inheritance with Moose

C.4 What About Moo?

C.5 Appendix Summary

C.6 References

D Perlbrew, CPAN, and cpanm

D.1 CPAN and @INC

D.1.1 Finding Modules

D.1.2 Using Modules

I Already Have It!

D.1.3 Package Manager

D.1.4 Manually: CPAN

local::lib

D.2 cpanm

D.3 Perlbrew

D.4 Caveats: C Dependencies

D.5 Windows

E Dancing with Perl

E.1 A New Dancer App

E.1.1 Verbs

E.1.2 Templating

E.1.3 Parameters

E.1.4 POST

EXERCISE E May I Have This Dance?

Index

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.237.29