Thông tin tài liệu
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
http://www.granavenida.com/perl). 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 jmerelo@geneura.ugr.es, and his
homepage (which is, so far, in Spanish) is at http://geneura.ugr.es/~jmerelo
Chris Nandor
Chris Nandor, pudge@pobox.com, 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
http://use.perl.org/ and http://news.perl.org/. 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
[...]... 'Win32::API' module Using Perl from C First Steps 872 874 875 875 875 Building a Program (The Hard Way) Building a Program (The Easy Way) 876 877 Implementing the Perl Interpreter Embedding Perl Code Getting Perl Values Using Perl Subroutines Working with Perl Internals Using Modules 877 880 881 882 884 886 The Java -Perl Lingo 888 xvii Table of Contents Perl and COM PerlCOM 'PerlCtrl' Miscellaneous Languages... of Perl programmers, this is a good thing; in the eyes of advocates of some other languages, it isn't Perl' s anti-motto really ought to be 'Just Because You Can Do It, Doesn't Mean You Should.'; Perl does not impose good programming practices, so it is also easy to write badly constructed and hard-to-read code through sloppy programming Perl is a very practically-minded language, and takes a no-frills... code on non-UNIX ports of Perl, notably ActivePerl's PPM package tool Finally, we look at the various ways of running Perl, and setting up the operating system to recognize Perl scripts as Perl scripts We also take a look at the Perl command line together with special environment variables, and examine one way to create stand-alone Perl applications that can run without the benefit of a Perl installation... of Perl is its implementation of libraries with modules, which has made it a genuinely extensible language With its clarity, logical structure, and practical approach, Professional Perl Programming is the ideal guide and companion into the world of Perl programming Who Is This Book For? The breadth and depth of this book make it immensely useful for both the newcomer to Perl and the experienced Perl. .. Over Reading Detecting the End-of-File Reading a Single Character Writing to Filehandles Buffering and Autoflush Mode Alternatives to 'print' Handling Binary and Text Files The 'binmode' Function The 'open' Pragma Random Access 'seek' to a Specific Place within a File Clearing the End-of-File Condition with 'seek' Writing at the End-of-File Finding the Current Position Object-Oriented Random Access 426... of professional developers sharing their hard earned knowledge Following in the Wrox tradition, this book goes beyond merely reprising the original (and free) documentation supplied with the language and applies a pragmatic, example based approach to every subject that it covers Whether Perl is an old acquaintance or a new frontier, any Perl developer will find plenty to discover in Professional Perl. .. programming with Perl objects We are shown how to write object classes and are given a detailed look at inheritance and subclassing The chapter covers keeping data private, destroying objects, and operator overloading The last section discusses a special feature of Perl: ties and tied objects Chapter 20 stops looking at Perl from the surface and takes us inside the Perl interpreter We examine how Perl. .. Chapter 14: Command-Line and Shell Interaction Parsing-Command Line Arguments Command-Line Conventions The '@ARGV' Array Passing Arguments to Perl Itself Setting Variables from '@ARGV' Reading Files from '@ARGV' '@ARGV' and Standard Input Simple Command-Line Processing with 'Getopt::Std' Basic Processing with 'getopt' Slightly Smarter Processing with 'getopts' More Complex Command-line Processing with... An Example Tied Hash Class An Example Class Using 'Tie::StdHash' Summary Chapter 20: Inside Perl Analyzing the Perl Binary – 'Config.pm' 'perl -V' How It Works Platform Compiler Linker and Libraries Dynamic Linking Under the Hood Around the Source Tree Building Perl 'metaconfig' Rather than 'autoconf'? How Perl Works Parsing Compiling Interpreting Internal Variable Types PVs IVs NVs Arrays and Hashes... create simple Perl shells and how to integrate the underlying shell into our Perl scripts The next chapter discusses programming terminal input and output We show different ways of writing to terminals, including using the Term::ReadKey and Term::ReadLine modules The chapter then addresses how to write to the screen and introduces high-level terminal modules Finally, Chapter 15 shows us the low-level interface . 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. 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. Team-Fly ® Professional Perl Programming © 2001 Wrox Press All rights reserved. No part of this book may be reproduced, stored
Ngày đăng: 25/03/2014, 10:29
Xem thêm: professional perl programming - wrox 2001