Beginning Perl Third Edition PHẦN 1 potx

47 430 0
Beginning Perl Third Edition PHẦN 1 potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lee Perl Companion eBook Available Beginning James Lee, Author of Hacking Linux Exposed this print for content only—size & color not accurate CYAN MAGENTA YELLOW BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Beginning Perl Dear Reader, Whether you are a complete novice or an experienced programmer, you hold in your hands the ideal guide to learning Perl. Originally created as a powerful text processing tool, Perl has since evolved into a multipurpose, multiplatform programming language capable of implementing a variety of tasks such as system administration, web and network programming, and XML processing. In this book I will provide valuable insight into Perl's role regarding several of these tasks and more. Starting with a comprehensive overview of the basics of Perl, I'll introduce important concepts such as Perl's data types and control flow constructs. This material sets the stage for a discussion of more complex topics, such as writing custom functions, using regular expressions, and file input and output. Next, we move on to the advanced topics of object-oriented programming, modules, CGI programming, and database administration with Perl's powerful database interface module, DBI. The examples and code provided offer you all of the information you need to start writing your own powerful scripts to solve the problems listed above, and many more. After years of experience programming in this powerful language, I've come to appreciate Perl's versatility and functionality for solving simple and highly complex problems alike. Plus, Perl is one of the most enjoyable languages to use—programming in Perl is fun! I am confident that once you have studied the material covered in this book, you'll feel the same. James Lee US $39.99 Shelve in: Perl User level: Beginning www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2793-9 9 781430 227939 5 39 9 9 THE APRESS ROADMAP The Definitive Guide to Catalyst Pro Perl Linux System Administration Recipes Beginning Perl 3rd Ed Beginning Portable Shell Scripting Covers Perl 5.10 THIRD EDITION 7.5 x 9.25 spine = 0.875" 464 page count THE EXPERT’S VOICE ® IN OPEN SOURCE Perl THIRD EDITION James Lee Perl for those who missed it the first time around: Learn about the duct tape for the web, the cloud and system administration Beginning Covers Perl 5.10 Beginning Perl Third Edition ■ ■ ■ JAMES LEE with SIMON COZENS www.wowebook.com ii Beginning Perl, Third Edtion Copyright © 2010 by James Lee All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2793-9 ISBN-13 (electronic): 978-1-4302-2794-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Frank Pohlmann Technical Reviewers: Richard Dice, Ed Schaefer, Todd Shandelman Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Laurin Becker Copy Editors: Katie Stence, Sharon Terdeman Compositor: Kimberly Burton Indexer: Brenda Miller Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. iii For Polly and Dave Pistole i v Contents at a Glance ■About the Author xvi ■About the Technical Reviewers xvii ■Acknowledgements xviii ■Introduction xix ■Chapter 1: First Steps in Perl 1 ■Chapter 2: Scalars 13 ■Chapter 3: Control Flow Constructs 53 ■Chapter 4: Lists and Arrays 81 ■Chapter 5: Hashes 115 ■Chapter 6: Subroutines/Functions 131 ■Chapter 7: Regular Expressions 153 ■Chapter 8: Files and Data 179 ■Chapter 9: String Processing 207 ■Chapter 10: Interfacing to the Operating System 215 ■Chapter 11: References 231 ■Chapter 12: Modules 257 ■Chapter 13: Object-Oriented Perl 287 ■Chapter 14: Introduction to CGI 317 ■Chapter 15: Perl and DBI 349 ■Appendix: Exercise Solutions 387 ■Index 409 v Contents ■About the Author xvi ■About the Technical Reviewers xvii ■Acknowledgements xviii ■Introduction xix ■Chapter 1: First Steps in Perl 1 Programming Languages 1 Our First Perl Program 2 Program Structure 6 Character Sets 8 Escape Sequences 8 Whitespace 9 Number Systems 9 The Perl Debugger 11 Summary 11 Exercises 12 ■Chapter 2: Scalars 13 Types of Data 13 Numbers 14 Strings 17 Here-Documents 20 Converting Between Numbers and Strings 21 ■ CONTENTS CONTENTS vi Operators 22 Numeric Operators 22 String Operators 32 Operators to Be Covered Later 36 Operator Precedence 37 Variables 38 Scoping 43 Variable Names 46 Variable Interpolation 46 Currency Converter 48 Two Miscellaneous Functions 50 The exit() Function 50 The die() Function 51 Summary 52 Exercises 52 ■Chapter 3: Control Flow Constructs 53 The if Statement 54 Operators Revisited 55 Multiple Choice: if . . . else 61 The unless Statement 64 Expression Modifiers 65 Using Short-Circuited Evaluation 65 Looping Constructs 66 The while Loop 66 while (<STDIN>) 67 Infinite Loops 69 Looping Until 70 The for Loop 71 ■ CONTENTS vii The foreach Loop 71 do while and do until 72 Loop Control Constructs 74 Breaking Out 74 Going On to the Next 75 Reexecuting the Loop 76 Loop Labels 77 goto 79 Summary 79 Exercises 79 ■Chapter 4: Lists and Arrays 81 Lists 81 Simple Lists 82 More Complex Lists 83 Creating Lists Easily with qw// 84 Accessing List Values 87 Arrays 91 Assigning Arrays 91 Scalar vs. List Context 94 Adding to an Array 95 Accessing an Array 95 Summary 114 Exercises 114 ■Chapter 5: Hashes 115 Creating a Hash 115 Working with Hash Values 117 Hash in List Context 119 ■ CONTENTS CONTENTS viii Hash in Scalar Context 120 Hash Functions 121 The keys() Function 121 The values() Function 122 The each() Function 123 The delete() Function 123 The exists() Function 124 Hash Examples 125 Creating Readable Variables 125 “Reversing” Information 125 Counting Things 126 Summary 129 Exercises 129 ■Chapter 6: Subroutines/Functions 131 Understanding Subroutines 132 Defining a Subroutine 132 Invoking a Subroutine 133 Order of Declaration and Invoking Functions 134 Passing Arguments into Functions 137 Return Values 139 The return Statement 141 Understanding Scope 142 Global Variables 142 Introduction to Packages 144 Lexical Variables (aka Local Variables) 146 Some Important Notes on Passing Arguments 147 Function Arguments Passed by Reference 147 Lists Are One-Dimensional 149 [...]... 15 0 Named Parameters 15 1 Summary .15 2 Exercises .15 2 ■Chapter 7: Regular Expressions 15 3 What Are They? 15 3 Patterns 15 4 Working with Regexes 17 0 Substitution 17 0 Changing Delimiters 17 2 Modifiers 17 3 The split() Function 17 4 The... Function 17 5 Common Blunders 17 5 Summary .17 6 Exercises .17 7 ■Chapter 8: Files and Data 17 9 Filehandles 17 9 The open() Function 17 9 The close() Function 18 0 Three Ways to Open a File 18 1 Read Mode 18 2 Reading in Scalar Context 18 3 Reading with the... collection of those Perl modules mentioned previously Other important Perl sites are listed here: • • www.theperlreview.com: The home of the Perl Review, an online Perl magazine • www.activestate.com: The home of Perl on Windows • xxvi www.pm.org: The Perl Mongers, a worldwide umbrella organization for Perl user groups • 1 www .perl. org: A site with tons of information about Perl www.perlarchive.com: Another... 3 01 Do You Need OO? 313 Are Your Subroutines Tasks? 314 Do You Need Persistence? 314 Do You Need Sessions? 314 Do You Need Speed? 314 Do You Want the User to Be Unaware of the Object? 314 Are You Still Unsure? 314 Summary . 315 Exercises . 315 ■Chapter 14 : Introduction... 18 5 @ARGV: The Command-Line Arguments 18 7 @ARGV and 18 9 $ARGV 19 0 Reading in List Context 19 0 ix ■ CONTENTS CONTENTS Writing to Files .19 2 Buffering 19 5 Opening Pipes 19 6 Receiving Piped Data from a Process 19 6 Sending Piped Data to Another Process 19 8 Bidirectional... enhancements to Perl syntax that are being debated—the Todo file in the Perl source kit explains what’s currently on the table Perl 6 The future of Perl lies in Perl 6, a complete rewrite of the language The purpose of Perl 6 is to address the problems with Perl 5 and to create a language that can continue to grow and change in the future Larry Wall has this to say about Perl 6: Perl 5 was my rewrite of Perl. .. 208 The rindex() Function 210 The substr() Function 210 Transliteration 212 Summary . 213 Exercises . 213 ■Chapter 10 : Interfacing to the Operating System 215 The %ENV Hash . 215 Working with Files and Directories 217 File Globbing with glob() 217 Reading Directories 220... interesting things, as you’re about to see Perldoc Typing perldoc perl from a command prompt presents the Perl documentation table of contents and some basic information about Perl The pages you’re probably going to use the most are the Perl FAQ and perlfunc, which describes the built-in functions Because of this, perldoc has a special interface to these two pages perldoc -f allows you to see information... snipped—try it yourself!): $ perldoc -f print print FILEHANDLE LIST print LIST print Prints a string or a list of strings Returns true if successful FILEHANDLE may be a scalar variable name, in which case [output snipped] Similarly, perldoc -q allows you to search the Perl FAQ for any regular expression or keyword $ perldoc -q reverse Found in /usr/lib /perl5 /5 .10 .1/ pod/perlfaq4.pod How do I reverse... same time makes a lot of the old code obsolete Perl 4 code may still run, but Perl 4 style is definitely frowned upon these days At the time of writing, the current stable release of Perl is 5 .10 .1, which is what this book will describe That said, the maintainers of Perl are very careful to ensure that old code will run, perhaps all the way back to Perl 1 changes and features that break existing programs . Summary 11 4 Exercises 11 4 ■Chapter 5: Hashes 11 5 Creating a Hash 11 5 Working with Hash Values 11 7 Hash in List Context 11 9 ■ CONTENTS CONTENTS viii Hash in Scalar Context 12 0 Hash. Parameters 15 1 Summary 15 2 Exercises 15 2 ■Chapter 7: Regular Expressions 15 3 What Are They? 15 3 Patterns 15 4 Working with Regexes 17 0 Substitution 17 0 Changing Delimiters 17 2 Modifiers 17 3. Functions 12 1 The keys() Function 12 1 The values() Function 12 2 The each() Function 12 3 The delete() Function 12 3 The exists() Function 12 4 Hash Examples 12 5 Creating Readable Variables 12 5

Ngày đăng: 09/08/2014, 14:21

Mục lục

  • Contents at a Glance

  • About the Technical Reviewers

  • Introduction

    • The Future of Perl—Developers Releases and Perl 6

    • It’s Open Source

    • Perl on the Web and the Network

    • Windows, Unix, and Other Operating Systems

    • What Do I Need to Use This Book?

    • How Do I Get Perl?

    • How to Get Help

    • Downloading This Book’s Example Source Code

    • Who This Book Is For

    • How This Book Is Organized

    • First Steps in Perl

      • Programming Languages

      • Our First Perl Program

        • Program Structure

        • Statements and Statement Blocks

        • Binary, Hexadecimal, and Octal Numbers

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan