📄 Page
1
(This page has no text content)
📄 Page
2
(This page has no text content)
📄 Page
3
FOURTH EDITION Programming Perl Tom Christiansen, brian d foy & Larry Wall with Jon Orwant Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo
📄 Page
4
Programming Perl, Fourth Edition by Tom Christiansen, brian d foy & Larry Wall, with Jon Orwant Copyright © 2012 Tom Christiansen, brian d foy, Larry Wall, and Jon Orwant. 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: Andy Oram Production Editor: Holly Bauer Proofreader: Marlowe Shaeffer Indexer: Lucie Haskins Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano January 1991: First Edition. September 1996: Second Edition. July 2000: Third Edition. February 2012: Fourth Edition. Revision History for the Fourth Edition: 2011-02-13 First release See http://oreilly.com/catalog/errata.csp?isbn=9780596004927 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming Perl, the image of a dromedary camel, 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 contained herein. ISBN: 978-0-596-00492-7 [M] 1329160875
📄 Page
5
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Part I. Overview 1. An Overview of Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Getting Started 3 Natural and Artificial Languages 4 Variable Syntax 5 Verbs 17 An Average Example 18 How to Do It 20 Filehandles 21 Operators 24 Some Binary Arithmetic Operators 25 String Operators 25 Assignment Operators 26 Unary Arithmetic Operators 28 Logical Operators 29 Some Numeric and String Comparison Operators 30 Some File Test Operators 31 Control Structures 31 What Is Truth? 32 The given and when Statements 34 Looping Constructs 35 Regular Expressions 39 Quantifiers 43 Minimal Matching 44 Nailing Things Down 44 Backreferences 45 iii
📄 Page
6
List Processing 47 What You Don’t Know Won’t Hurt You (Much) 49 Part II. The Gory Details 2. Bits and Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Atoms 53 Molecules 54 Built-in Data Types 56 Variables 58 Names 60 Name Lookups 62 Scalar Values 65 Numeric Literals 67 String Literals 67 Pick Your Own Quotes 70 Or Leave Out the Quotes Entirely 72 Interpolating Array Values 73 “Here” Documents 73 Version Literals 75 Other Literal Tokens 76 Context 76 Scalar and List Context 76 Boolean Context 78 Void Context 79 Interpolative Context 79 List Values and Arrays 79 List Assignment 82 Array Length 83 Hashes 84 Typeglobs and Filehandles 86 Input Operators 87 Command Input (Backtick) Operator 87 Line Input (Angle) Operator 88 Filename Globbing Operator 91 3. Unary and Binary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Terms and List Operators (Leftward) 97 The Arrow Operator 99 Autoincrement and Autodecrement 100 iv | Table of Contents
📄 Page
7
Exponentiation 101 Ideographic Unary Operators 101 Binding Operators 103 Multiplicative Operators 104 Additive Operators 105 Shift Operators 105 Named Unary and File Test Operators 106 Relational Operators 111 Equality Operators 111 Smartmatch Operator 112 Smartmatching of Objects 117 Bitwise Operators 118 C-Style Logical (Short-Circuit) Operators 119 Range Operators 120 Conditional Operator 123 Assignment Operators 125 Comma Operators 126 List Operators (Rightward) 127 Logical and, or, not, and xor 127 C Operators Missing from Perl 128 4. Statements and Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Simple Statements 130 Compound Statements 131 if and unless Statements 133 The given Statement 133 The when Statement and Modifier 137 Loop Statements 139 while and until Statements 139 Three-Part Loops 140 foreach Loops 142 Loop Control 144 Bare Blocks as Loops 147 Loopy Topicalizers 149 The goto Operator 149 Paleolithic Perl Case Structures 150 The Ellipsis Statement 152 Global Declarations 153 Scoped Declarations 155 Scoped Variable Declarations 156 Table of Contents | v
📄 Page
8
Lexically Scoped Variables: my 159 Persistent Lexically Scoped Variables: state 160 Lexically Scoped Global Declarations: our 161 Dynamically Scoped Variables: local 162 Pragmas 164 Controlling Warnings 165 Controlling the Use of Globals 165 5. Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 The Regular Expression Bestiary 168 Pattern-Matching Operators 171 Pattern Modifiers 175 The m// Operator (Matching) 181 The s/// Operator (Substitution) 184 The tr/// Operator (Transliteration) 189 Metacharacters and Metasymbols 192 Metasymbol Tables 193 Specific Characters 199 Wildcard Metasymbols 200 Character Classes 202 Bracketed Character Classes 202 Classic Perl Character Class Shortcuts 204 Character Properties 207 POSIX-Style Character Classes 210 Quantifiers 214 Positions 217 Beginnings: The \A and ^ Assertions 218 Endings: The \z, \Z, and $ Assertions 218 Boundaries: The \b and \B Assertions 219 Progressive Matching 219 Where You Left Off: The \G Assertion 220 Grouping and Capturing 221 Capturing 221 Grouping Without Capturing 229 Scoped Pattern Modifiers 230 Alternation 231 Staying in Control 232 Letting Perl Do the Work 233 Variable Interpolation 234 The Regex Compiler 239 vi | Table of Contents
📄 Page
9
The Little Engine That /Could(n’t)?/ 241 Fancy Patterns 247 Lookaround Assertions 247 Possessive Groups 249 Programmatic Patterns 251 Recursive Patterns 260 Grammatical Patterns 262 Defining Your Own Assertions 270 Alternate Engines 271 6. Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Show, Don’t Tell 280 Getting at Unicode Data 282 The Encode Module 285 A Case of Mistaken Identity 287 Graphemes and Normalization 290 Comparing and Sorting Unicode Text 297 Using the UCA with Perl’s sort 303 Locale Sorting 305 More Goodies 306 Custom Regex Boundaries 308 Building Character 309 References 313 7. Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Syntax 315 Semantics 317 Tricks with Parameter Lists 318 Error Indications 320 Scoping Issues 321 Passing References 324 Prototypes 326 Inlining Constant Functions 331 Care with Prototypes 332 Prototypes of Built-in Functions 333 Subroutine Attributes 335 The method Attribute 335 The lvalue Attribute 336 Table of Contents | vii
📄 Page
10
8. References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 What Is a Reference? 339 Creating References 342 The Backslash Operator 342 Anonymous Data 342 Object Constructors 345 Handle References 346 Symbol Table References 347 Implicit Creation of References 348 Using Hard References 348 Using a Variable As a Variable Name 348 Using a BLOCK As a Variable Name 349 Using the Arrow Operator 350 Using Object Methods 352 Pseudohashes 352 Other Tricks You Can Do with Hard References 353 Closures 355 Symbolic References 359 Braces, Brackets, and Quoting 360 References Don’t Work As Hash Keys 361 Garbage Collection, Circular References, and Weak References 362 9. Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 Arrays of Arrays 365 Creating and Accessing a Two-Dimensional Array 366 Growing Your Own 366 Access and Printing 368 Slices 370 Common Mistakes 371 Hashes of Arrays 374 Composition of a Hash of Arrays 374 Generation of a Hash of Arrays 374 Access and Printing of a Hash of Arrays 375 Arrays of Hashes 376 Composition of an Array of Hashes 376 Generation of an Array of Hashes 377 Access and Printing of an Array of Hashes 377 Hashes of Hashes 378 Composition of a Hash of Hashes 378 Generation of a Hash of Hashes 379 viii | Table of Contents
📄 Page
11
Access and Printing of a Hash of Hashes 380 Hashes of Functions 381 More Elaborate Records 382 Composition, Access, and Printing of More Elaborate Records 382 Composition, Access, and Printing of Even More Elaborate Records 383 Generation of a Hash of Complex Records 384 Saving Data Structures 385 10. Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Symbol Tables 389 Qualified Names 393 The Default Package 394 Changing the Package 395 Autoloading 397 11. Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 Loading Modules 402 Unloading Modules 404 Creating Modules 405 Naming Modules 405 A Sample Module 405 Module Privacy and the Exporter 406 Overriding Built-in Functions 411 12. Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Brief Refresher on Object-Oriented Lingo 415 Perl’s Object System 417 Method Invocation 418 Method Invocation Using the Arrow Operator 419 Method Invocation Using Indirect Objects 421 Syntactic Snafus with Indirect Objects 421 Package-Quoted Classes 423 Object Construction 424 Inheritable Constructors 425 Initializers 427 Class Inheritance 429 Inheritance Through @ISA 430 Alternate Method Searching 432 Accessing Overridden Methods 433 UNIVERSAL: The Ultimate Ancestor Class 435 Table of Contents | ix
📄 Page
12
Method Autoloading 438 Private Methods 440 Instance Destructors 440 Garbage Collection with DESTROY Methods 441 Managing Instance Data 442 Generating Accessors with Autoloading 444 Generating Accessors with Closures 445 Using Closures for Private Objects 446 New Tricks 449 Managing Class Data 450 The Moose in the Room 453 Summary 455 13. Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 The overload Pragma 458 Overload Handlers 459 Overloadable Operators 460 The Copy Constructor (=) 468 When an Overload Handler Is Missing (nomethod and fallback) 469 Overloading Constants 470 Public Overload Functions 472 Inheritance and Overloading 472 Runtime Overloading 473 Overloading Diagnostics 473 14. Tied Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Tying Scalars 477 Scalar-Tying Methods 478 Magical Counter Variables 483 Cycling Through Values 483 Magically Banishing $_ 484 Tying Arrays 486 Array-Tying Methods 487 Notational Convenience 491 Tying Hashes 492 Hash-Tying Methods 493 Tying Filehandles 498 Filehandle-Tying Methods 499 Creative Filehandles 506 A Subtle Untying Trap 510 x | Table of Contents
📄 Page
13
Tie Modules on CPAN 512 Part III. Perl as Technology 15. Interprocess Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 Signals 518 Signalling Process Groups 520 Reaping Zombies 521 Timing Out Slow Operations 522 Blocking Signals 522 Signal Safety 523 Files 523 File Locking 524 Passing Filehandles 528 Pipes 531 Anonymous Pipes 531 Talking to Yourself 533 Bidirectional Communication 536 Named Pipes 538 System V IPC 540 Sockets 543 Networking Clients 545 Networking Servers 547 Message Passing 550 16. Compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 The Life Cycle of a Perl Program 554 Compiling Your Code 556 Executing Your Code 562 Compiler Backends 564 Code Generators 565 The Bytecode Generator 566 The C Code Generators 566 Code Development Tools 567 Avant-Garde Compiler, Retro Interpreter 569 17. The Command-Line Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 Command Processing 575 #! and Quoting on Non-Unix Systems 578 Location of Perl 580 Table of Contents | xi
📄 Page
14
Switches 580 Environment Variables 594 18. The Perl Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603 Using the Debugger 604 Debugger Commands 606 Stepping and Running 607 Breakpoints 607 Tracing 609 Display 609 Locating Code 610 Actions and Command Execution 611 Miscellaneous Commands 613 Debugger Customization 615 Editor Support for Debugging 615 Customizing with Init Files 616 Debugger Options 616 Unattended Execution 619 Debugger Support 620 Writing Your Own Debugger 622 Profiling Perl 623 Devel::DProf 623 Devel::NYTProf 627 19. CPAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629 History 629 A Tour of the Repository 630 Creating a MiniCPAN 632 The CPAN Ecosystem 633 PAUSE 633 Searching CPAN 635 Testing 635 Bug Tracking 635 Installing CPAN Modules 636 By Hand 637 CPAN Clients 638 Creating CPAN Distributions 640 Starting Your Distribution 640 Testing Your Modules 642 xii | Table of Contents
📄 Page
15
Part IV. Perl as Culture 20. Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 Handling Insecure Data 648 Detecting and Laundering Tainted Data 651 Cleaning Up Your Environment 656 Accessing Commands and Files Under Reduced Privileges 657 Defeating Taint Checking 660 Handling Timing Glitches 661 Unix Kernel Security Bugs 662 Handling Race Conditions 663 Temporary Files 665 Handling Insecure Code 668 Changing Root 669 Safe Compartments 670 Code Masquerading As Data 675 21. Common Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679 Common Goofs for Novices 679 Universal Blunders 680 Frequently Ignored Advice 682 C Traps 683 Shell Traps 684 Python Traps 685 Ruby Traps 687 Java Traps 689 Efficiency 691 Time Efficiency 691 Space Efficiency 697 Programmer Efficiency 698 Maintainer Efficiency 698 Porter Efficiency 699 User Efficiency 700 Programming with Style 701 Fluent Perl 705 Program Generation 715 Generating Other Languages in Perl 716 Generating Perl in Other Languages 717 Source Filters 718 Table of Contents | xiii
📄 Page
16
22. Portable Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721 Newlines 723 Endianness and Number Width 724 Files and Filesystems 725 System Interaction 727 Interprocess Communication (IPC) 727 External Subroutines (XS) 728 Standard Modules 728 Dates and Times 729 Internationalization 729 Style 730 23. Plain Old Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731 Pod in a Nutshell 731 Verbatim Paragraphs 733 Command Paragraphs 733 Flowed Text 737 Pod Translators and Modules 740 Writing Your Own Pod Tools 742 Pod Pitfalls 747 Documenting Your Perl Programs 748 24. Perl Culture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 History Made Practical 751 Perl Poetry 754 Virtues of the Perl Programmer 756 Events 757 Getting Help 758 Part V. Reference Material 25. Special Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763 Special Names Grouped by Type 763 Regular Expression Special Variables 763 Per-Filehandle Variables 764 Per-Package Special Variables 764 Program-Wide Special Variables 765 Per-Package Special Filehandles 766 Per-Package Special Functions 766 Special Variables in Alphabetical Order 767 xiv | Table of Contents
📄 Page
17
26. Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793 String Formats 793 Binary Formats 799 pack 800 unpack 809 Picture Formats 810 Format Variables 814 Footers 817 Accessing Formatting Internals 817 27. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819 Perl Functions by Category 822 Perl Functions in Alphabetical Order 824 28. The Standard Perl Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991 Library Science 991 A Tour of the Perl Library 993 Roll Call 995 The Future of the Standard Perl Library 997 Wandering the Stacks 998 29. Pragmatic Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001 attributes 1002 autodie 1003 autouse 1004 base 1005 bigint 1006 bignum 1006 bigrat 1007 blib 1007 bytes 1007 charnames 1008 Custom Character Names 1009 Runtime Lookups 1010 constant 1012 Restrictions on constant 1013 deprecate 1014 diagnostics 1014 encoding 1017 feature 1017 Table of Contents | xv
📄 Page
18
fields 1018 filetest 1018 if 1019 inc::latest 1019 integer 1019 less 1020 lib 1021 locale 1022 mro 1023 open 1023 ops 1024 overload 1025 overloading 1025 parent 1026 re 1026 sigtrap 1029 Signal Handlers 1029 Predefined Signal Lists 1030 Other Arguments to sigtrap 1030 Examples of sigtrap 1031 sort 1032 strict 1032 strict "refs" 1033 strict "vars" 1033 strict "subs" 1034 subs 1035 threads 1035 utf8 1037 vars 1037 version 1037 vmsish 1038 exit 1038 hushed 1038 status 1039 time 1039 warnings 1039 User-Defined Pragmas 1042 xvi | Table of Contents
📄 Page
19
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045 Index of Perl Modules in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1091 Table of Contents | xvii
📄 Page
20
(This page has no text content)