Beginning Perl

pdf
Số trang Beginning Perl 415 Cỡ tệp Beginning Perl 10 MB Lượt tải Beginning Perl 0 Lượt đọc Beginning Perl 0
Đánh giá Beginning Perl
4.2 ( 15 lượt)
Nhấn vào bên dưới để tải tài liệu
Đang xem trước 10 trên tổng 415 trang, để tải xuống xem đầy đủ hãy nhấn vào bên trên
Chủ đề liên quan

Nội dung

CYAN MAGENTA YELLOW BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Companion eBook Available Beginning Perl Perl 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. Beginning Hacking Linux Exposed Covers Perl 5.10 Dear Reader, James Lee, Author of THE EXPERT’S VOICE ® IN OPEN SOURCE James Lee Beginning Perl THE APRESS ROADMAP Companion eBook Beginning Perl 3rd Ed Beginning Portable Shell Scripting See last page for details on $10 eBook version Perl for those who missed it the first time around: Learn about the duct tape for the web, the cloud and system administration Pro Perl Linux System Administration Recipes The Definitive Guide to Catalyst THIRD EDITION THIRD EDITION Covers Perl 5.10 SOURCE CODE ONLINE ISBN 978-1-4302-2793-9 5 39 9 9 US $39.99 James Lee Lee www.apress.com Shelve in: Perl User level: Beginning 9 781430 227939 this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.875" 464 page count Beginning Perl Third Edition ■■■ JAMES LEE with SIMON COZENS www.wowebook.com 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@springersbm.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. ii For Polly and Dave Pistole iii 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 iv 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 v ■ CONTENTS CONTENTS 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 () ..................................................................................................................................... 67 Infinite Loops .......................................................................................................................................... 69 Looping Until........................................................................................................................................... 70 The for Loop............................................................................................................................................ 71 vi ■ CONTENTS 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 vii ■ CONTENTS CONTENTS 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 viii
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.