Programming Perl, 3Rd Edition
Total Page:16
File Type:pdf, Size:1020Kb
Programming Perl Programming Perl Third Edition Larry Wall, Tom Christiansen & Jon Orwant Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Programming Perl, Third Edition by Larry Wall, Tom Christiansen, and Jon Orwant Copyright © 2000, 1996, 1991 O’Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472. Editor, First Edition: Tim O’Reilly Editor, Second Edition: Steve Talbott Editor, Third Edition: Linda Mui Technical Editor: Nathan Torkington Production Editor: Melanie Wang Cover Designer: Edie Freedman Printing History: January 1991: First Edition. September 1996: Second Edition. July 2000: Third Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly & Associates, 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 & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. The association between the image of a camel and the Perl language is a trademark of O’Reilly & Associates, Inc. Permission may be granted for non-commercial use; please inquire by sending mail to [email protected]. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Library of Congress Cataloging-in-Publication Data Wall, Larry. Programming Perl/Larry Wall, Tom Christiansen & Jon Orwant.--3rd ed. p. cm. ISBN 0-596-00027-8 1. Perl (Computer program language) I. Christiansen, Tom. II. Orwant, Jon. III. Title. QA76.73.P22 W35 2000 005.13'3--dc21 00-055799 ISBN: 0-596-00027-8 [M] Ta ble of Contents Preface ................................................................................................................. xvii I: Overview .................................................................................................. 1 1: An Over view of Perl .................................................................................... 3 Getting Started .................................................................................................... 3 Natural and Artificial Languages ........................................................................ 4 An Average Example ........................................................................................ 17 Filehandles ........................................................................................................ 20 Operators .......................................................................................................... 22 Contr ol Structur es ............................................................................................. 29 Regular Expressions ......................................................................................... 35 List Processing .................................................................................................. 41 What You Don’t Know Won’t Hurt You (Much) ............................................. 43 II : The Gor y Details ........................................................................... 45 2: Bits and Pieces ........................................................................................... 47 Atoms ................................................................................................................ 47 Molecules .......................................................................................................... 49 Built-in Data Types ........................................................................................... 50 Variables ............................................................................................................ 52 Names ............................................................................................................... 53 Scalar Values ..................................................................................................... 58 Context .............................................................................................................. 69 v vi Table of Contents List Values and Arrays ...................................................................................... 72 Hashes ............................................................................................................... 76 Typeglobs and Filehandles .............................................................................. 78 Input Operators ................................................................................................ 79 3: Unary and Binary Operator s .............................................................. 86 Terms and List Operators (Leftward) .............................................................. 89 The Arrow Operator ......................................................................................... 90 Autoincr ement and Autodecrement ................................................................ 91 Exponentiation ................................................................................................. 92 Ideographic Unary Operators .......................................................................... 92 Binding Operators ............................................................................................ 93 Multiplicative Operators ................................................................................... 94 Additive Operators ........................................................................................... 95 Shift Operators .................................................................................................. 95 Named Unary and File Test Operators ............................................................ 95 Relational Operators ....................................................................................... 100 Equality Operators ......................................................................................... 101 Bitwise Operators ........................................................................................... 101 C-Style Logical (Short-Circuit) Operators ...................................................... 102 Range Operator .............................................................................................. 103 Conditional Operator ..................................................................................... 105 Assignment Operators .................................................................................... 107 Comma Operators .......................................................................................... 108 List Operators (Rightward) ............................................................................. 109 Logical and, or, not, and xor ......................................................................... 109 C Operators Missing from Perl ...................................................................... 110 4: Statements and Declarations ............................................................ 111 Simple Statements .......................................................................................... 111 Compound Statements ................................................................................... 113 if and unless Statements ................................................................................ 114 Loop Statements ............................................................................................. 115 Bar e Blocks ..................................................................................................... 123 goto ................................................................................................................. 126 Global Declarations ........................................................................................ 127 Scoped Declarations ....................................................................................... 129 Pragmas ........................................................................................................... 136 Ta ble of Contents vii 5: Patter n Matching .................................................................................... 139 The Regular Expression Bestiary ................................................................... 140 Patter n-Matching Operators ........................................................................... 143 Metacharacters and Metasymbols .................................................................. 158 Character Classes ............................................................................................ 165 Quantifiers ...................................................................................................... 176 Positions .......................................................................................................... 178 Capturing and Clustering ............................................................................... 182 Alter nation ...................................................................................................... 187 Staying in Control ........................................................................................... 188 Fancy Patterns ...............................................................................................