Đây là bộ sách tiếng anh cho dân công nghệ thông tin chuyên về bảo mật,lập trình.Thích hợp cho những ai đam mê về công nghệ thông tin,tìm hiểu về bảo mật và lập trình.
www.it-ebooks.info www.it-ebooks.info Learning Perl Downloa d f r o m W o w ! e B o o k < w w w.woweb o o k . c o m > www.it-ebooks.info www.it-ebooks.info SIXTH EDITION Learning Perl Randal L. Schwartz, brian d foy, and Tom Phoenix Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo www.it-ebooks.info Learning Perl, Sixth Edition by Randal L. Schwartz, brian d foy, and Tom Phoenix Copyright © 2011 Randal L. Schwartz, brian d foy, and Tom Phoenix. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Simon St.Laurent Production Editor: Kristen Borg Copyeditor: Audrey Doyle Proofreader: Kiel Van Horn Indexer: John Bickelhaupt Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: November 1993: First Edition. July 1997: Second Edition. July 2001: Third Edition. July 2005: Fourth Edition. July 2008: Fifth Edition. June 2011: Sixth Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning Perl, the image of a llama, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-1-449-30358-7 [LSI] 1308077187 www.it-ebooks.info Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Questions and Answers 1 Is This the Right Book for You? 1 Why Are There So Many Footnotes? 2 What About the Exercises and Their Answers? 3 What Do Those Numbers Mean at the Start of the Exercise? 4 What If I’m a Perl Course Instructor? 4 What Does “Perl” Stand For? 4 Why Did Larry Create Perl? 5 Why Didn’t Larry Just Use Some Other Language? 5 Is Perl Easy or Hard? 6 How Did Perl Get to Be So Popular? 7 What’s Happening with Perl Now? 7 What’s Perl Really Good For? 8 What Is Perl Not Good For? 8 How Can I Get Perl? 9 What Is CPAN? 10 How Can I Get Support for Perl? 10 Are There Any Other Kinds of Support? 10 What If I Find a Bug in Perl? 12 How Do I Make a Perl Program? 12 A Simple Program 13 What’s Inside That Program? 15 How Do I Compile My Perl Program? 16 A Whirlwind Tour of Perl 17 Exercises 18 2. Scalar Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Numbers 21 v www.it-ebooks.info All Numbers Have the Same Format Internally 22 Floating-Point Literals 22 Integer Literals 22 Nondecimal Integer Literals 23 Numeric Operators 23 Strings 24 Single-Quoted String Literals 25 Double-Quoted String Literals 25 String Operators 26 Automatic Conversion Between Numbers and Strings 27 Perl’s Built-in Warnings 28 Scalar Variables 29 Choosing Good Variable Names 30 Scalar Assignment 31 Binary Assignment Operators 31 Output with print 32 Interpolation of Scalar Variables into Strings 32 Creating Characters by Code Point 34 Operator Precedence and Associativity 34 Comparison Operators 36 The if Control Structure 37 Boolean Values 38 Getting User Input 39 The chomp Operator 39 The while Control Structure 40 The undef Value 41 The defined Function 42 Exercises 42 3. Lists and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Accessing Elements of an Array 44 Special Array Indices 45 List Literals 46 The qw Shortcut 46 List Assignment 48 The pop and push Operators 49 The shift and unshift Operators 50 The splice Operator 50 Interpolating Arrays into Strings 51 The foreach Control Structure 53 Perl’s Favorite Default: $_ 54 The reverse Operator 54 The sort Operator 54 vi | Table of Contents www.it-ebooks.info The each Operator 55 Scalar and List Context 55 Using List-Producing Expressions in Scalar Context 57 Using Scalar-Producing Expressions in List Context 58 Forcing Scalar Context 59 <STDIN> in List Context 59 Exercises 60 4. Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Defining a Subroutine 63 Invoking a Subroutine 64 Return Values 64 Arguments 66 Private Variables in Subroutines 68 Variable-Length Parameter Lists 69 A Better &max Routine 69 Empty Parameter Lists 70 Notes on Lexical (my) Variables 71 The use strict Pragma 72 The return Operator 74 Omitting the Ampersand 74 Non-Scalar Return Values 76 Persistent, Private Variables 76 Exercises 78 5. Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Input from Standard Input 81 Input from the Diamond Operator 83 The Invocation Arguments 85 Output to Standard Output 86 Formatted Output with printf 89 Arrays and printf 90 Filehandles 91 Opening a Filehandle 93 Binmoding Filehandles 95 Bad Filehandles 96 Closing a Filehandle 96 Fatal Errors with die 97 Warning Messages with warn 99 Automatically die-ing 99 Using Filehandles 100 Changing the Default Output Filehandle 100 Reopening a Standard Filehandle 101 Table of Contents | vii www.it-ebooks.info Output with say 102 Filehandles in a Scalar 103 Exercises 104 6. Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 What Is a Hash? 107 Why Use a Hash? 109 Hash Element Access 110 The Hash As a Whole 112 Hash Assignment 113 The Big Arrow 114 Hash Functions 115 The keys and values Functions 115 The each Function 116 Typical Use of a Hash 118 The exists Function 118 The delete Function 118 Hash Element Interpolation 119 The %ENV hash 119 Exercises 120 7. In the World of Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 What Are Regular Expressions? 121 Using Simple Patterns 122 Unicode Properties 123 About Metacharacters 123 Simple Quantifiers 124 Grouping in Patterns 125 Alternatives 127 Character Classes 128 Character Class Shortcuts 129 Negating the Shortcuts 131 Exercises 131 8. Matching with Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Matches with m// 133 Match Modifiers 134 Case-Insensitive Matching with /i 134 Matching Any Character with /s 134 Adding Whitespace with /x 135 Combining Option Modifiers 135 Choosing a Character Interpretation 136 Other Options 138 viii | Table of Contents www.it-ebooks.info [...]... is also good Perl support on Stack Overflow (http://www stackoverflow.com) You can also check out http://learn .perl. org/ and its associated mailing list, beginners @perl. org Many well-known Perl programmers also have blogs that regularly feature Perl- related posts, most of which you can read through Perlsphere, http:/ /perl sphere.net/ If you find yourself needing a support contract for Perl, there are... perl5 -porters @perl. org mailing list As we write this (March 2011), there is a lot happening with Perl For the past couple of years, many people have been working on the next major version of Perl: Perl 6 In short, Perl 6 is a completely different language now, even to the point that it’s main implementation goes by the name Rakudo In 2000, Perl 6 started as something that might replace Perl 5, which... there wasn’t time to update it before Perl 5.12 came out Now this book is available right around the time Perl 5.14 should be released, with the Perl 5 Porters already thinking about Perl 5.16 What’s Perl Really Good For? Perl is good for quick-and-dirty programs that you whip up in three minutes Perl is also good for long-and-extensive programs that will take a dozen programmers three years to finish... documentation, you can also read the documentation on CPAN, http://www.cpan.org, as well as other sites—http://perldoc .perl. org has HTML and PDF versions of the Perl documentation, and http://faq .perl. org/ has the latest version of the perlfaq Another authoritative source is the book Programming Perl, commonly called “the Camel book” because of its cover animal (just as this book is known as “the Llama... comp.lang .perl. announce, which is a low-volume newsgroup just for important announcements about Perl, including any security-related announcements Ask your local expert if you need help with Usenet Also, a few web communities have sprung up around Perl discussions One very popular one, known as The Perl Monastery (http://www.perlmonks.org), has seen quite a bit of participation from many Perl book... been waiting for you to pitch a second book—Programming perl is selling like gangbusters.” That started the effort over the next 18 months to finish the first edition of Learning Perl During that time, I was starting to see an opportunity to teach Perl classes outside Silicon Valley,† so I created a class based on the text I was writing for Learning Perl I gave a dozen classes for various clients (including... at you Perl is a little like that Is Perl Easy or Hard? Perl is easy to use, but sometimes hard to learn This is a generalization, of course In designing Perl, Larry made many trade-offs When he’s had the chance to make something easier for the programmer at the expense of being more difficult for the student, he’s decided in the programmer’s favor nearly every time That’s because you’ll learn Perl only... the Web because Perl is a great tool for leverage Perl s concise constructs allow you to create (with minimal fuss) some very cool one-up solutions or general tools Also, you can drag those tools along to your next job because Perl is highly portable and readily available, so you’ll have even more time to surf Perl is a very high-level language That means that the code is quite dense; a Perl program may... Happening with Perl Now? Larry Wall doesn’t write the code these days, but he still guides the development and makes the big decisions Perl is mostly maintained by a hardy group of people § With a sharp jump when any one section of the program exceeds the size of your screen What Does Perl Stand For? | 7 www.it-ebooks.info called the Perl 5 Porters You can follow their work and discussions on the perl5 -porters @perl. org... www.it-ebooks.info Preface Welcome to the sixth edition of Learning Perl, updated for Perl 5.14 and its latest features This book is still good even if you are still using Perl 5.8 (although, it’s been a long time since it was released; have you thought about upgrading?) If you’re looking for the best way to spend your first 30 to 45 hours with the Perl programming language, you’ve found it In the pages . 6 How Did Perl Get to Be So Popular? 7 What’s Happening with Perl Now? 7 What’s Perl Really Good For? 8 What Is Perl Not Good For? 8 How Can I Get Perl? 9 What. If I’m a Perl Course Instructor? 4 What Does Perl Stand For? 4 Why Did Larry Create Perl? 5 Why Didn’t Larry Just Use Some Other Language? 5 Is Perl Easy