Learning_Perl_6th_Edition.pdf
Learning perl SIXTH EDITION Learning Perl Randal l. schwartz, brian d foy, and Tom Phoenix ○ REILLY Beijing· Cambridge· Farnham·Koln· Sebastopol· Tokyo Learning perl sixth Edition by Randal L. Schwartz, brian d foy, and Tom Phoenix Copyright o 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 arealsoavailableformosttitles(http://my.safaribooksonline.com).Formoreinformationcontactour corporate/institutionalsalesdepartment:(800)998-9938orcorporate@oreilly.com Editor: Simon St laurent Indexer: John Bickelhaupt Production editor: Kristen be Cover designer Karen montgomery Copyeditor Audrey doyle Interior Designer: David Futato Proofreader: Kiel Van horn Illustrator: Robert ro 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 rademarks. 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 ILSI 1308077187 Table of contents Preface 1. Introducti Questions and Answers Is This the right Book for You? Why Are There So Many Footnotes? What about the Exercises and Their answers What Do those numbers mean at the Start of the exercise? What If i'm a perl course instructor? 4 What does“Perl” Stand for? Why Did Larry Create Perl? Why Didnt Larry Just Use Some Other Language? Is Perl easy or hard? How Did Perl get to Be so popular? What's Happening with Perl now 4556778 What's Perl Really Good For What is perl not good for? How Can i get perl? What IS CPAN? 10 How Can I Get Support for perl? 10 Are There any Other Kinds of Support? What If I Find a Bug in Perl? 12 How Do I Make a Perl Program A Simple program 13 What's Inside That Program? How do I Compile my perl Program? A Whirlwind tour of perl Exercises 18 2. Scalar data 21 umbers 21 All Numbers Have the Same Format Internally Floating-Point Literals 22 Integer literals Nondecimal Integer literals Numeric Operators Strings 24 Single-Quoted String Literals Double-Quoted String Literals 25 String Operators 26 Automatic Conversion Between Numbers and Strings 27 Perl's Built-in warnings Scalar variables 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 The if Control Structure 37 Boolean values 38 Getting User Input 39 The chomp Operator 39 The while Control Structure The undef value The defined Function Exercises 42 3. Lists and arrays Accessing Elements of an arra pecial array Indic 45 46 The gw s qY hortcut List Assignment The pop and push Operators 49 The shift and unshift Operators 50 The splice operator 50 Interpolating Arrays into Strings 51 The foreach Control Str 53 Perl,'s Favorite Default: S 54 The reverse Operator 54 The sort Operator 54 ⅵ i Table of Contents The each Operator 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 in List context Exercises 60 4. Subroutines∴ Defining a Subroutine 63 evoking a Subroutine 64 Return values 64 Arguments 66 Private variables in subroutines 68 Variable-Length Parameter Lists 6 a better &max routine Empty Parameter Lists Notes on Lexical (my) variables The use strict Pragma 72 The return Operator 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 Output to Standard Output Formatted Output with printf Arrays and printf Filehandles 91 Opening a filehandle 93 Binmoding Filehandles 95 Bad Filehandles Closing a filehandle 96 Fatal errors with die Warning messages with warn Automatically die-ing Using Filehandles 100 Changing the Default Output Filehandle 100 Reopening a Standard Filehandle 101 Table of contents|ⅶi Output with sa 102 Filehandles in a scalar 103 Exercises 104 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 ypical Use of a Hash 118 The exists Function 118 The delete Fu 118 Hash Element Interpolation 119 The %enV hash Exercises 120 7. In the World of Regular expressions ,121 What Are Regular Expressions? 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 Negating the shortcuts 131 Exercises 131 8. Matching with reqular Expressions ,133 Matches with m// 133 Match modifiers 134 Case-Insensitive Matching with / 134 Matching Any Character with /s 134 Adding whitespace with /x 135 Combining Option Modifiers 135 Choosing a Character Interpretation 136 Other Options 138 ⅶ ii Table of Contents
暂无评论