Tải bản đầy đủ (.pdf) (1,200 trang)

professional perl programming - wrox 2001

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (10.5 MB, 1,200 trang )

Professional Perl Programming
Peter Wainwright
with
Aldo Calpini
Arthur Corliss
Simon Cozens
Juan Julián Merelo Guervós
Chris Nandor
Aalhad Saraf
Wrox Press Ltd.
TEAMFLY























































Team-Fly
®

Professional Perl Programming
© 2001 Wrox Press
All rights reserved. No part of this book may be reproduced, stored in a retrieval system or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embodied in critical articles or reviews.
The author and publisher have made every effort in the preparation of this book to ensure the
accuracy of the information. However, the information contained in this book is sold without
warranty, either express or implied. Neither the authors, Wrox Press nor its dealers or
distributors will be held liable for any damages caused or alleged to be caused either directly or
indirectly by this book.
Published by Wrox Press Ltd,
Arden House, 1102 Warwick Road, Acocks Green,
Birmingham, B27 6BH, UK
Printed in the United States
ISBN 1-861004-49-4
Trademark Acknowledgements
Wrox has endeavored to provide trademark information about all the companies and products
mentioned in this book by the appropriate use of capitals. However, Wrox cannot guarantee the
accuracy of this information.
Credits
Author Technical Reviewers
Peter Wainwright Simon Cozens
Carl Culvett
Contributing Authors

David Fannin
Aldo Calpini
Iain Georgeson
Arthur Corliss
Terry Gliedt
Simon Cozens
Jim Harle
Juan Julián Merelo Guervós
Chris Lightfoot
Chris Nandor
Mark Mamone
Aalhad Saraf Neil Matthews
Bill Moss
Technical Architect
Nathan Neulinger
Louay Fatoohi Gavin Smyth
Rick Stones
Technical Editors
Paul Warren
Mankee Cheng Andrew Yourtchenko
David Mercer Philips Yuson
Andrew Polshaw
Dan Robotham Production Manager
Simon Hardware
Additional Editorial
Mohammed Rfaquat
Production Project Coordinator
Mark Burdett
Author Agents
Julia Gilbert

Illustrations
Velimir Ilic Shabnam Hussain
Project Administrator Cover
Nicola Phillips Shelley Frazier
Indexers Proof Readers
Alessandro Ansa Chris Smith
Adrian Axinte Keith Westmoreland
Bill Johncocks Diana Skeldon
Category Managers
Viv Emery
Paul Cooper
About the Authors
Peter Wainwright
Peter Wainwright is a freelance developer and software consultant. He got his first taste of
programming on a BBC Micro and gained most of his early programming experience writing
applications in C on Solaris. He then discovered Linux, shortly followed by Perl and Apache,
and has been happily programming there ever since.
Outside of the software industry, he is a partner of Space Future Consulting, an international
space tourism consultancy firm. He spends much of his free time maintaining the non-profit
Space Future website at www.spacefuture.com and writes the occasional article on space
tourism. He is also an advisor to the board of one or two companies engaged in space tourism
and vehicle development. If you have $50m to spare, he would like to have a word with you.
As well as being the primary author of Professional Perl Programming, he is the author of
Professional Apache (ISBN: 1861003021), also published by Wrox Press, as well as a contributing
author to Beginning Per l (ISBN: 1861003145). Formerly based in London, he recently moved
from England to the Upper West Side of New York to be with his new wife, a professional editor,
whom he met through a mutual interest in space tourism and low-rent apartments.
Aldo Calpini
Aldo Calpini is well known in the Perl community for his many important Win32 modules. His
active participation on several mailing lists has helped the Perl language grow in the Win32

community. He began programming twenty years ago and still enjoys hacking every kind of
computer he can put his hands on. He works today as lead programmer in an Italian Internet
startup company.
Arthur Corliss
Arthur Corliss has been programming since buying his first home computer a Timex Sinclair
1000 with a whopping 2K of RAM (which he still has). Having worked his way through several
languages, Perl has become his most frequent language of choice at his latest venture, Gallant
Technologies, Inc., a software development company. On his own time he continues the
madness by working on the Curses::Widgets and Curses::Forms modules, which he
authored and is available on CPAN.
Simon Cozens
Simon Cozens is an Open Source programmer and author; he writes for the Perl Journal,
www.perl.com, and other sites, and is the author of Wrox Press' Beginning Perl (ISBN:
1861003145). He is a member of the Perl development team, and his hobbies include reading,
typography and the Greek language and culture.
Juan Julián Merelo Guervós
Juan Julián Merelo Guervós was born in Ubeda, Jaén, Spain, the 10th of March of 1965. Juan
received a degree in Theoretical Physics by the University of Granada in 1989, and a PhD in
Physics in 1994. He has been hacking PERL since 1993 or 1994, and is the author of a widely
popular (and pioneer) web tutorial of PERL in Spanish (available from
Currently Juan is an associate professor at Granada
University, in Spain. He is married and has three wonderful daughters, two of whom are
fraternal twins, born in 1998 and 1999. He can be reached at , and his
homepage (which is, so far, in Spanish) is at />Chris Nandor
Chris Nandor, , is a programmer for OSDN, working on theSlashdot code.
He co-authored the book MacPerl: Power and Ease (ISBN
: 1881957322) from Prime Time
Freeware, writes the Perl News column for the Perl Journal, and runs the web sites
and Chris lives in Massachusetts with his three dogs,
two cats, and one wonderful and supportive wife, Jennifer.

I'd like to dedicate this book to my wife, my parents, and my in-laws who've all given me encouragement &
put up with me during deadlines.
Aalhad Saraf
Aalhad Saraf is in the Systems Software Group in IBM Labs. He has been with Linux since 1995.
Perl, C, and C++ are his favorite tools. Has a Bachelors degree in Electronics Engineering from
the University of Pune, a Post Graduate Diploma awarded by the national resource center of the
'Center for Development of Advanced Computing' (a scientific society of the Ministry of IT,
Govt. of India). He worked on microcontrollers/embedded systems and hand-held computing
devices in Syslab Automation and on an interactive gaming server for DishnetDSL – one of
India's leading ISPs. He also teaches Perl, Software Engineering and Quality systems during his
spare time. He Takes his table tennis seriously, fiddles around with a guitar, juggles tennis balls
and is an omnivore when it comes to reading. He likes networks. He dreams of traveling through
space sitting in a packet; playing with the civilization of Data, providing the subjects – bytes, with
new worlds and increasingly better means of transport. Aalhad is 22 years old and aspires to get
a Ph.D. in Computer Science some day.

Table of Contents
Introduction 1
Chapter 1: Introduction 7
Introduction 7
Key Features 8
Supported Platforms 9
Perl History and Versions 10
Essential Information 10
Building and Installing Perl 12
Installing Pre-built Perl Distributions 12
Installing Perl on UNIX 13
Installing Perl on Windows 13
Installing Perl on Macintosh 14
Building Perl from Source 14

Building a Threaded Perl Interpreter 17
Differences for Windows and Macintosh 17
Other Platforms 17
Building and Installing Modules 18
Installing Modules with the 'CPAN' Module 18
Starting and Configuring CPAN 19
Installing Modules 20
Listing and Searching CPAN 21
Reading Module Documentation Without Installing 23
Reloading CPAN 23
Configuring CPAN Options 24
Installing Modules by Hand 24
Running Perl 27
Starting Perl Applications 27
Perl on UNIX 28
Perl on Windows 28
Perl on Macintosh 29
The Command Line 29
Command Line Syntax 30
Supplying Arguments to Perl Scripts 30
Using Perl as a Generic Command Line Utility 31
The Perl Environment 32
General Environment Variables Used by Perl 32
Perl Specific Environment Variables 32
Summary 33
Table of Contents
ii
Chapter 2: Basic Concepts 35
Values and Variables 35
Whitespace 36

Data Types 36
Special Variables 39
Interpolation 39
Context 40
Scalar and List Context 41
Void Context 42
Operators 42
Blocks 43
Control Constructs 43
Loop Modifiers 45
Subroutines 46
Functions 47
Scoping 47
Chapter 3: Scalars 51
Value Conversion and Caching 51
Numbers 52
Integers 53
Integer Range and Big Integers 53
Converting Integers into Floating-Point Numbers 54
Converting Integers into Strings 54
Handling Different Number Bases 55
Floating-Point Numbers 56
Converting Floats into Integers 58
Converting Floats into Strings 58
The 'use integer' Pragma 60
Mathematical Functions 61
Strings 62
Quotes and Quoting 62
'Here' Documents 65
Bareword Strings and Version Numbers 67

Converting Strings into Numbers 68
Converting Strings into Lists and Hashes 69
Functions For Manipulating Strings 71
Print 71
Line Terminator Termination 72
Characters and Character Codes 73
Length and Position 73
Substrings and Vectors 74
Upper and Lower Case 75
Interpolation 75
Pattern Matching and Transliteration 75
Password Encryption 76
Low Level String Conversions 76
String Formatting 80
Summary 84
Table of Contents
iii
Chapter 4: Operators 87
Operators Versus Functions 87
Operator Types and Categories 88
Assignment 89
Arithmetic 89
Shift 91
String and List 91
Logical 93
Bitwise 94
Combination Assignment 97
Increment and Decrement 98
Comparison 99
Regular Expression Binding 101

Comma and Relationship 101
Reference and Dereference 102
The Arrow 102
Range 103
Ternary 105
Precedence and Associativity 106
Precedence and Parentheses 107
Disabling Functions and Operators 110
Overriding Operators 113
Summary 114
Chapter 5: Beyond Scalars – More Data Types 117
Lists and Arrays 117
Manipulating Arrays 119
Modifying the Contents of an Array 119
Counting an Array 121
Adding Elements to an Array 122
Resizing and Truncating an Array 123
Removing Elements from an Array 123
Removing All Elements from an Array 125
Sorting and Reversing Lists and Arrays 125
Changing the Starting Index Value 127
Converting Lists and Arrays into Scalars 127
Taking References 127
Converting Lists into Formatted Strings 127
Hashes 129
Manipulating Hashes 131
Adding and Modifying Hash Values 131
Removing Hash Keys and Values 132
Converting Lists and Arrays into Hashes 132
Reversing Hashes 133

Accessing and Iterating Over Hashes 134
Converting Hashes into Scalars 137
Converting Hashes into Arrays 138
The Special Hash '%ENV' 138
Configuring Programs via '%ENV' 140
Handling Tainted Input from '%ENV' 140
'Env.pm' 141
Table of Contents
iv
References 142
Hard References 142
Creating References 143
Comparing References 144
Dereferencing 145
Working with References 146
Passing References to Subroutines 147
Finding the Type of a Reference 148
Complex Data Structures 151
The Problem with Nesting – My Lists Went Flat! 151
Lists of Lists and Multidimensional Arrays 151
Hashes of Hashes and Other Animals 153
Adding to and Modifying Complex Data Structures 154
Creating Complex Data Structures Programmatically 155
Traversing Complex Data Structures 158
Typeglobs 161
Defining Typeglobs 161
Manipulating Typeglobs 162
Accessing Typeglobs 163
The Undefined Value 164
Tests of Existence 166

Using the Undefined Value 167
Using 'undef' as a Function 168
Constants 169
Declaring Scalar Constants with the 'constant' Pragma 169
Declaring List and Hash Constants 171
Constant References 171
Listing and Checking for the Existence of Constants 172
Summary 172
Chapter 6: Structure, Flow, and Control 175
Expressions, Statements, and Blocks 175
Declarations 176
Expressions and Simple Statements 176
Blocks and Compound Statements 177
Blocks in Perl Statements 178
Naked Blocks 178
Defining the Main Program as a Block 179
Blocks as Loops 180
The 'do' Block 181
Special Blocks 182
Conditional Statements 183
What Is Truth? 184
'if', 'else', and 'elsif' 185
'unless' 187
Writing Conditions with Logical Operators 188
The Ternary Operator 189
Switches and Multi-Branched Conditions 192
Returning Values from Multi-Branched Conditions 194
Table of Contents
v
Loops and Looping 195

Writing C-style Loops with 'for' 195
Writing Better Loops with 'foreach' 197
Using 'foreach' with Multi-Branched Conditions 198
Variable Aliasing in 'foreach' Loops 199
Conditional Loops – 'while', 'until', and 'do' 200
Variable Aliasing with 'while' 201
Looping over Lists and Arrays with 'while' 201
Looping on Self-Modifying Arrays 202
Looping over Hashes with 'while' 203
'do while' and 'do until' 203
Controlling Loop Execution 204
The 'continue' Clause 206
Controlling Nested Loops 207
The Trouble with 'do' 208
The 'goto' Statement 209
'maps' and 'greps' 210
'map' 211
'grep' 211
Summary 213
Chapter 7: Subroutines 215
Declaring and Calling Subroutines 216
Anonymous Subroutines and Subroutine References 217
Strict Subroutines and the 'use strict subs' Pragma 219
Predeclaring Subroutines 219
Overriding Built-in Functions 220
The Subroutine Stack 221
Recursion 223
Checking for Subroutines and Defining Subroutines On the Fly 226
Passing Parameters 228
Passing Lists and Hashes 229

Converting Scalar Subroutines into List Processors 231
Passing '@_' Directly into Subroutines 232
Named Parameters 233
Prototypes 236
Defining the Number of Parameters and Their Scope 237
Prototyping Code References 239
Subroutines as Scalar Operators 239
Requiring Variables Rather than Values 240
Optional Parameters 241
Disabling Prototypes 242
Returning Values from Subroutines 243
Returning the Undefined Value 244
Determining and Responding to the Calling Context 246
Handling Void Context 247
Handling Context: an Example 247
Closures 248
Assignable Subroutines 250
TEAMFLY























































Team-Fly
®

Table of Contents
vi
Attribute Lists 251
Defining Attributes on Subroutines 251
Accessing Attributes 252
Special Attributes 252
Package Attributes 253
Summary 254
Chapter 8: Scope and Visibility 257
Package Variables 257
Defining Package Variables 258
Using 'strict' Variables 259
Declaring Global Package Variables 260
Declaring Global Package Variables with 'use vars' 260
Lexically Declaring Global Package Variables with 'our' 261
Automatic Localization in Perl 262

Localizing Package Variables with 'local' 262
Lexical Variables 264
Declaring Lexical Variables 265
Preserving Lexical Variables Outside Their Scope 266
The Symbol Table 267
The 'main' Package 267
Typeglobs and the 'main' Package 268
The Symbol Table Hierarchy 269
Manipulating the Symbol Table Directly 270
Accessing the Symbol Table 271
Summary 273
Chapter 9: Using Modules 275
Modules and Packages 276
Loading Code Using 'do', 'require', and 'use' 277
'import' Lists 278
Suppressing Default Imports 279
Disabling Features with 'no' 279
Testing for Module Versions and the Version of Perl 279
Pragmatic Modules 280
How Pragmatic Modules Work 280
Scope of Pragmatic Modules 281
The Special Hash '%INC' 282
The Special Array '@INC' 283
Modifying '@INC' directly 283
Modifying @INC with the 'lib' Pragma 284
Locating Libraries Relative to the Script 285
Checking for the Availability of a Module 286
Finding Out What Modules are Installed 286
Postponing Module Loading Until Use 290
Summary 292

Table of Contents
vii
Chapter 10: Inside Modules and Packages 295
'BEGIN' blocks, 'END' blocks and Other Animals 296
'BEGIN' Blocks 297
'END' Blocks 299
'CHECK' and 'INIT' 299
Manipulating Packages 300
Removing a Package 302
Forbidding Package Variables 303
Finding a Package Name Programmatically 303
Autoloading 304
Autoloading Subroutines 305
Using an 'AUTOLOAD' Subroutine as a Substitute 305
Defining Subroutines On the Fly 308
Self-Defining Instead of Autoloading Subroutines 309
Autoloading Modules 310
Using the 'AutoLoader' Module 311
Using the 'SelfLoader' Module 314
Importing and Exporting 316
The 'import' Mechanism 316
Exporting 319
Setting Flags with Exports 321
When to Export, When not to Export 321
The 'Exporter' Module 322
Creating Installable Modules 330
Well-Written Modules 330
Creating a Working Directory 331
Building an Installable Package 332
Adding a Test Script 334

Uploading Modules to CPAN 334
Summary 335
Chapter 11: Regular Expressions 337
String Interpolation 337
Perl's Interpolation Syntax 337
Interpolating Metacharacters and Character Codes 338
Common Special Characters 339
Special Effects 339
Interpolating Variables 340
Interpolating Code 341
Interpolative Context 342
Interpolation in Regular Expressions 342
Interpolating Text Inside String Variables 343
Protecting Strings Against Interpolation 344
Regular Expressions 345
Where Regular Expressions Occur 346
Matching and Substitution 346
The 'split' Function 347
Pre-compiled Regular Expressions 348
Table of Contents
viii
Regular Expression Delimiters 349
Elements of Regular Expressions 350
More Advanced Patterns 352
Matching Arbitrary Characters and Character Classes 352
Repetition and Grouping 354
Matching Sequential and Overlapping Terms 361
Pattern Match Modifiers 362
Regular Expressions versus Wildcards 363
Metacharacters 365

Character Class Metacharacters 365
Zero-Width Metacharacters 368
Extracting Matched Text 369
Special Variables 369
Parentheses and Numbered Variables 371
Backreferences 374
Extracting Lines with the Range Operator 376
Matching More than Once 378
Regular Expressions and Loops 378
Nested Regular Expression Loops 380
Position 382
Matching In and Across Multiple Lines 384
Counting the Total Number of Matches 386
Overlapping Matches and Zero-Width Patterns 386
Extended Patterns 389
Documenting Regular Expressions 389
Writing Efficient Regular Expressions 391
Making Regular Expressions More Specific 391
'study' 393
Avoiding Recompilation with the Empty Pattern 394
Avoiding Recompilation with the 'once-only' Modifier 394
Generating Regular Expressions with 'eval' 395
Predefining Search Patterns with 'qr' 397
Checking the Validity of Regular Expressions 399
Regular Expressions, Tainted Variables, and Debugging 401
Untainting Tainted Variables 401
Maintaining Taintedness in Regular Expressions 401
Understanding and Debugging the Regular Expression Engine 402
Substitution 407
Substituting Matched Text and Interpolation 407

Evaluating the Replacement String 408
Interpolating and Evaluating the Replacement String 409
Transliteration 409
Transliteration Modifiers 411
Summary 412
Chapter 12: Input and Output with Filehandles 415
IO and Filehandles 416
The Filehandle Data Type 416
Standard Filehandles 416
Table of Contents
ix
Creating Filehandles 417
Creating Filehandles with 'open' 417
Opening Files for Reading, Writing, and Updating 418
Opening Arbitrary Filenames 419
Opening Standard Input and Standard Output 419
Creating Filehandles with 'IO::File' 420
The Data Filehandle 422
Other Filehandles 423
Referring to Filehandles 424
Reading from Filehandles 426
The Readline Operator 426
Redefining the Line Separator 426
Aliasing Readline in 'while' Loops 427
Counting Line Numbers 427
Readline and the '@ARGV' Array 428
Finer Control Over Reading 428
Detecting the End-of-File 429
Reading a Single Character 430
Writing to Filehandles 430

Buffering and Autoflush Mode 432
Alternatives to 'print' 432
Handling Binary and Text Files 433
The 'binmode' Function 433
The 'open' Pragma 434
Random Access 434
'seek' to a Specific Place within a File 434
Clearing the End-of-File Condition with 'seek' 435
Writing at the End-of-File 436
Finding the Current Position 437
Object-Oriented Random Access 437
Truncating and Resizing Files 437
File Locking 439
Establishing File Locks 440
File Locking Issues and Caveats 442
Changing the Default Output Filehandle 442
Using Special Variables to Configure Other Filehandles 443
Automatically Restoring the Default Filehandle 443
Using Default Variables vs, 'IO::Handle' Methods 444
Duplicating and Aliasing Filehandles 444
Redirecting Filehandles 445
Caching Many Filehandles 446
'IO::Handle' Methods and Special Variables 448
Table of Contents
x
System Level IO 452
Opening Filehandles at the System Level 453
Open Mode Flags 453
Non-blocking IO 454
The Permissions Mask 455

Using 'sysopen' via 'IO::File' 456
Unbuffered Reading 456
Unbuffered Writing 457
System-Level File Positioning 458
'fcntl' and 'ioctl' 459
Setting Filehandle Attributes with 'fcntl' 459
Controlling Devices with 'ioctl' 462
POSIX IO 462
POSIX Filehandle Routines 463
POSIX File Descriptor Routines 463
Moving between POSIX and Standard IO 464
Summary 465
Chapter 13: Manipulating Files and Directories 467
Files and Filenames 467
Getting User and Group Information 468
User Information 468
Group Information 473
The Unary File Test Operators 475
Link Transparency and Testing for Links 477
Testing Binary and Text Files 477
Reusing the Results of a Prior 'stat' or 'lstat' 478
Using 'stat' Objects 479
Access Control Lists, the Superuser, and the 'filestat' Pragma 480
Automating Multiple File Tests 482
Interrogating Files 484
Changing File Attributes 485
File Ownership 485
File Permissions 487
The 'Fcntl' Module 488
Linking, Unlinking, and Renaming Files 490

Linking Files 491
Unlinking Files 491
Renaming Files 492
Symbolic Links 492
Copying and Moving Files 493
System Level Copies and Platform Portability 495
Comparing Files 496
Finding Files 497
Deciphering File Paths 501
Filename Globbing 503
'glob' Syntax 504
UNIX Style File Globbing 505
DOS-Style File Globbing 507
Temporary Files 508
Creating a Temporary Filehandle 508
Temporary Filenames via the POSIX module 509
Using 'File::Temp' 511
Table of Contents
xi
Manipulating Directories 513
Reading Directories 513
Directory Positions 514
Directory Handle Objects 515
Directories as Tied Hashes 515
Finding the Name of a Directory or File from its Handle 516
Creating and Destroying Directories 517
Creating Single Directories 517
Creating Multiple Directories 518
Destroying Single Directories 519
Destroying Multiple Directories 520

Moving Around Directories 520
Summary 523
Chapter 14: Command-Line and Shell Interaction 525
Parsing-Command Line Arguments 525
Command-Line Conventions 526
The '@ARGV' Array 527
Passing Arguments to Perl Itself 528
Setting Variables from '@ARGV' 528
Reading Files from '@ARGV' 529
'@ARGV' and Standard Input 531
Simple Command-Line Processing with 'Getopt::Std' 532
Basic Processing with 'getopt' 532
Slightly Smarter Processing with 'getopts' 534
More Complex Command-line Processing with 'Getopt::Long' 535
Simple Argument Processing 535
Option Prefixes 537
Defining Option Values 538
Abbreviations 541
Aliases 541
Handling Option Values 542
Documenting Options 544
Bundling Options 544
Case Sensitivity 546
Handling Unrecognized Option and Value Arguments 547
'POSIX' Mode 549
Summary of Configuration Options 549
Shells, Shell Commands, and Perl 551
Creating a Simple Perl Shell 551
Writing a More Useful Shell 552
Integrating the Shell into Perl 555

Emulating UNIX Commands on Windows 557
Summary 559
Table of Contents
xii
Chapter 15: Terminal Input and Output 561
Talking to Terminals 561
Determining if a Script is Interactive 562
Reading from the Keyboard 563
Simple Input 563
Controlling Terminal Input with 'Term::ReadKey' 564
Read Modes 564
Reading a Single Character 566
Reading Complete Lines 568
Passwords and Invisible Input 569
Finding and Setting the Screen Size 569
Serial Connections and Terminal Speed 571
Line Ending Translation 571
Getting and Setting Control Characters 572
Advanced Line Input with 'Term::ReadLine' 574
Creating a Terminal Object 575
Supported Features 577
Setting the Prompt Style and Supplying Default Input 578
Command-Line History 579
Word Completion 580
Writing to the Screen 581
Terminal Capabilities 581
Creating a Termcap Object 582
Clearing the Screen, Moving the Cursor, and Other Tricks 584
Writing in Colors 584
Higher-Level Terminal Modules 588

'Term::Screen' 588
The Curses Library 590
A Simple Curses Application 591
Curses Windows 593
Third-Party Extensions to Curses 594
Programming the Terminal Directly via 'POSIX' 594
Summary 597
Chapter 16: Warnings and Errors 599
Enabling Warnings 599
Enabling Diagnostics 600
Generating Warnings and Errors 602
Intercepting Warnings and Errors 603
Deciphering Error Results from System Calls 604
Error Numbers and Names 604
Setting the Error Number 605
Errors from Evaluated Code 605
Extended Error Messages 606
'Errno' and The 'POSIX' Module 607
Checking the Exit Status of Subprocesses and External Commands 607
Table of Contents
xiii
Making Non-Fatal Errors Fatal 608
Returning Warnings and Errors in Context with 'Carp' 609
Error Logs and System Logs 611
Advanced Warnings 612
Summary 615
Chapter 17: Debugging 617
Pragmatic Debugging Support 617
Applications that Debug Themselves 618
A Simple Debugging System 619

A Better Debugging System 619
Creating Debug Logs 621
Adding Calling Context to Debug Messages 621
The Perl Debugger 622
Starting the Debugger 622
Entering Commands 623
Simple Debugging Commands – Single Stepping 624
Running Arbitrary Code 625
Multiline Commands 625
Debugging Commands 626
Configuring the Debugger 636
Interactively 636
Through the Environment 636
The '.perldb' Script 637
Debugger Configuration Options 638
Tracing and Terminals 641
Entering the Debugger Programmatically 642
Using Debugger Hooks 643
Debugging and Informational Modules 645
The 'Dumpvalue' Module 645
The 'Safe' Module 649
Creating Compartments 649
Sharing Variables and Subroutines 649
Operator Masks 650
The 'Safe' Module in Use 651
Debugging the Perl Interpreter 652
Automated Testing 653
Writing a Test Script 653
Automating Tests 655
Profiling 656

Profiling Perl Applications 657
Generating Profile Reports 657
Collecting Timing Information Directly 661
Testing for Performance 663
'timeit' 665
'timethis' 666
'timethese' 667
'cmpthese' 667
'countit' 668
Summary 669
Table of Contents
xiv
Chapter 18: Text Processing and Document Generation 671
Text Processing 672
Expanding and Contracting Tabs with 'Text::Tabs' 672
Calculating Abbreviations with 'Text::Abbrev' 673
Parsing Words and Phrases with 'Text::ParseWords' 675
Parsing Space-Separated Text 676
Parsing Arbitrarily Delimited Text 677
Batch Parsing Multiple Lines 678
Parsing a Single Line Only 679
Formatting Paragraphs with 'Text::Wrap' 679
Formatting Single Paragraphs 679
Customized Wrapping 680
Formatting Whole Documents 681
Formatting on the Command Line 681
Matching Similar Sounding Words with 'Text::Soundex' 682
Tokenizing Single Words 682
Tokenizing Lists of Words and E-Mail Addresses 683
The 'Soundex' Algorithm 684

Handling Untokenizable Words 684
Other Text Processing Modules 684
Documenting Perl 684
Comments 685
plain old documentation 685
pod Paragraphs 685
Using pod with 'DATA' or 'END' tokens 690
Interior Sequences 691
Pod Tools and Utilities 691
Translator Tools 691
Retrieval Tools 692
pod Verification 692
Programming pod 694
Using Pod Parsers 695
Writing a pod Parser 695
Locating pods 698
Reports – The 'r' in Perl 699
Formats and the Format Datatype 699
Formats and Filehandles 701
Assigning Formats to Standard Output 702
Determining and Assigning Formats to Other Filehandles 703
Format Structure 704
Picture Lines and Placeholders 704
Data Lines 707
Suppressing Redundant Lines 708
Autorepeating Pattern Lines 709
Page Control 709
Creating Footers 711
Combining Reports and Regular Output 712
Mixing 'write' and 'print' 712

Generating Report Text with 'formline' 714
Summary 715
Table of Contents
xv
Chapter 19: Object-Oriented Perl 717
Introducing Objects 718
Object Concepts 718
Classes 718
Objects 718
Inheritance, Multiple Inheritance, and Abstraction 719
Public and Private Methods and Data 720
Polymorphism 720
Overloading 720
Adaptabilty (also called Casting or Conversion) 721
Programming with Objects 721
Creating Objects 721
Using Objects 722
Accessing Properties 722
Calling Class Methods 723
Calling Object Methods 724
Nesting Method Calls 724
Determining what an Object Is 724
Determining Inherited Characteristics 725
Determining an Object's Ancestry 725
Determining an Object's Capabilities 725
Determining an Object's Version 726
Writing Object Classes 727
Constructors 727
Choosing a Different Underlying Data Type 729
Methods 732

Object Data 736
Class Data 740
Debugging Object Classes 747
Inheritance and Subclassing 753
Inheriting from a Parent Class 754
Overriding and Calling Overridden Methods 754
Overriding and Calling Overridden Constructors 755
Writing Inheritable Classes 756
More on Class Data through Objects 756
Exports 759
Private Methods 760
Extending and Redefining Objects 761
Extending a Parent Class 761
Constructing Subclasses On the Fly 762
Multiple Inheritance 764
Writing Classes for Multiple Inheritance 765
Drawbacks with Multiple Inheritance 767
A 'UNIVERSAL' Constuctor 768
Is-a Versus Has-a 770
Autoloading Methods 773
Keeping Data Private 777
Private Class Data 778
Private Object Data 778
TEAMFLY























































Team-Fly
®

Table of Contents
xvi
Destroying Objects 782
Destructors and Inheritance 783
Destructors and Multiple Inheritance 784
Overloading Operators 785
Basic Overloading 785
Determining the Operand Order and Operator Name 786
Overloading Comparisons 787
Overloading Conversion Operations 788

Overloading String Conversion 788
Overloading Numeric Conversion 790
Falling Back to Unoverloaded Operations 791
Overloading and Inheritance 793
Autogenerated Operations 793
Overloadable Operations 795
Automating Object Class Development 795
Ties and Tied Objects 799
Using Tied Objects 800
Handling Errors from 'tie' 800
Accessing Nested References 801
Testing a Variable for 'tied'ness 801
'Untie'ing Objects 802
Writing Tied Objects 802
Standard Tie Object Classes 803
Tied Object Methods 803
An Example Tied Hash Class 807
An Example Class Using 'Tie::StdHash' 809
Summary 811
Chapter 20: Inside Perl 815
Analyzing the Perl Binary – 'Config.pm' 815
'perl -V' 816
How It Works 817
Platform 817
Compiler 817
Linker and Libraries 818
Dynamic Linking 818
Under the Hood 818
Around the Source Tree 818
Building Perl 821

'metaconfig' Rather than 'autoconf'? 821
How Perl Works 822
Parsing 822
Compiling 824
Interpreting 826
Internal Variable Types 827
PVs 828
IVs 828
NVs 829
Arrays and Hashes 830
Examining Raw Datatypes with 'Devel::Peek' 830
Table of Contents
xvii
The Perl Compiler 833
The 'O' Module 834
The 'B' Module 834
The 'B::' Family of Modules 836
'B::Terse' 836
'B::Debug' 837
'B::Deparse' 839
'B::C' 840
'B::CC' 841
'B::Bytecode' 842
'B::Disassembler' 843
'B::Lint' 843
'B::Showlex' 845
'B::Xref' 845
'B::Fathom' 848
'B::Graph' 848
'B::JVM::Jasmin' 849

Writing a Perl Compiler Backend 850
Summary 853
Chapter 21: Integrating Perl with Other Programming Languages 855
Using C from Perl 856
An Overview of XS 857
'h2xs', or Where to Start 857
Converting C Header Files 858
Starting from Scratch 860
The XS File 860
Declaring the 'MODULE' and 'PACKAGE' 861
XS Functions 862
Putting the 'CODE' in 862
Complex Output, 'PPCODE' and the Stack 863
Complex Input, Default Values and Variable Input Lists 865
The 'TYPEMAP' 867
The 'Makefile' 870
Dynamic Linking 871
Using the 'FFI' Module 872
Using the 'C::DynaLib' Module 874
Using the 'Win32::API' module 875
Using Perl from C 875
First Steps 875
Building a Program (The Hard Way) 876
Building a Program (The Easy Way) 877
Implementing the Perl Interpreter 877
Embedding Perl Code 880
Getting Perl Values 881
Using Perl Subroutines 882
Working with Perl Internals 884
Using Modules 886

The Java-Perl Lingo 888
Table of Contents
xviii
Perl and COM 889
PerlCOM 889
'PerlCtrl' 890
Miscellaneous Languages 891
The Filter Module 892
Summary 893
Chapter 22: Creating and Managing Processes 895
Signals 895
Signal Handling 898
The 'die' and 'warn' Handlers 900
Writing Signal Handlers 900
Avoid Complex Handlers 901
Flexibly Installing Signal Handlers 903
Sending Signals 905
Alarms 906
A Simple Use of Alarms 906
Using Alarms to Abort Hung Operations 907
Starting New Processes 908
Replacing the Current Process with Another 909
Process IDs 909
Process, Groups and Daemons 909
Handling Children and Returning Exit Codes 911
Waiting for an Only Child 912
Getting the Exit Status 912
Handling Multiple Child Processes 913
POSIX Flags and Functions 914
Communicating Between Processes 916

Very Simple Solutions 916
Pipes 917
Opening and Running External Processes 919
Bi-directional Pipes 920
Avoiding Shells with the Forked Open 922
Bi-directional Pipes to External Processes 923
Handling Bi-directional Communications 925
Sharing Data Between Processes 926
IPC::SysV 927
Messages Queues 928
Semaphores 931
Shared Memory Segments 935
Threads 937
Checking for Thread Support 938
Creating Threads 938
Identifying Threads 939
Thread-specific Data 940
Thread Management 940
Variable Locks 942
Condition Variables, Semaphores, and Queues 943
Condition Variables 943
Semaphores 947
Queues 950
Thread Safety and Locked Code 953
Summary 954
Table of Contents
xix
Chapter 23: Networking with Perl 957
An Introduction to Networks 957
Protocol Layers 957

Frames, Packets, and Encapsulation 959
The Internet Protocol 960
UDP & TCP 964
ICMP 965
Other Protocols 966
Presentation/Application Layers 966
Anonymous, Broadcast, and Loopback Addresses 966
Networking with Perl 967
Sockets 967
'Socket.pm' 967
'IO::Socket.pm' 976
TCP INET Examples 979
UDP INET Examples 986
UNIX Sockets 989
Multiplexed Servers 992
Polling Servers 993
The 'select' Function 993
The' IO::Select' Module 995
A Simple Polling Server 996
A Simple Forking Server 998
A Simple Threaded Server 999
Getting Network Information 1000
System Network Files 1001
Hosts 1003
Networks 1007
Network Services 1009
Network Protocols 1011
Determining the Local Hostname 1012
Summary 1013
Chapter 24: Writing Portable Perl 1015

Maybe We Shouldn't Bother? 1016
Newlines 1016
Files and File Systems 1018
Portable File Handling with 'File::Spec' 1020
Endianness and Number Width 1023
System Interaction 1024
Inter-Process Communication (IPC) 1025
External Subroutines (XS) 1025
Modules 1026

×