Prepared exclusively for none ofyourbusiness Prepared exclusively for none ofyourbusiness Early Praise for Modern Perl, Fourth Edition A dozen years ago I was sure I knew what Perl looked like: unreadable and obscure. chromatic showed me beautiful, structured expressive code then. He’s the right guy to teach Modern Perl. He was writing it before it existed. ➤ Daniel Steinberg President, DimSumThinking, Inc. A tour de force of idiomatic code, Modern Perl teaches you not just “how” but also “why.” ➤ David Farrell Editor, PerlTricks.com If I had to pick a single book to teach Perl 5, this is the one I’d choose. As I read it, I was reminded of the first time I read K&R. It will teach everything that one needs to know to write Perl 5 well. ➤ David Golden Member, Perl 5 Porters, Autopragmatic, LLC I’m about to teach a new hire Perl using the first edition of Modern Perl. I’d much rather use the updated copy! ➤ Belden Lyman Principal Software Engineer, MediaMath It’s not the Perl book you deserve. It’s the Perl book you need. ➤ Gizmo Mathboy Co-founder, Greater Lafayette Open Source Symposium (GLOSSY) Prepared exclusively for none ofyourbusiness We've left this page blank to make the page numbers the same in the electronic and paper books. We tried just leaving it out, but then people wrote us to ask about the missing pages. Anyway, Eddy the Gerbil wanted to say “hello.” Prepared exclusively for none ofyourbusiness Modern Perl, Fourth Edition chromatic The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina Prepared exclusively for none ofyourbusiness 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 The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at https://pragprog.com. The team that produced this book includes: Michael Swaine (editor) Potomac Indexing, LLC (index) Linda Recktenwald (copyedit) Dave Thomas (layout) Janet Furlow (producer) Ellie Callahan (support) For international rights, please contact [email protected]. Copyright © 2015 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-68050-088-2 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—October 2015 Prepared exclusively for none ofyourbusiness Contents Preface . ix 1. The Perl Philosophy . 1 Perldoc 1 Expressivity 3 Context 5 Implicit Ideas 8 2. Perl and Its Community . 13 The CPAN 13 Community Sites 16 Development Sites 16 Events 17 IRC 17 3. The Perl Language . 19 Names 19 Variables 22 Values 24 Control Flow 35 Scalars 50 Arrays 52 Hashes 58 Coercion 66 Packages 68 References 71 Nested Data Structures 78 4. Operators . 85 Operator Characteristics 85 Operator Types 87 Prepared exclusively for none ofyourbusiness Contents • vi 5. Functions . 91 Declaring Functions 91 Invoking Functions 91 Function Parameters 92 Functions and Namespaces 97 Reporting Errors 98 Advanced Functions 99 Pitfalls and Misfeatures 103 Scope 104 Anonymous Functions 108 Closures 112 State versus Closures 116 State versus Pseudo-State 117 Attributes 118 AUTOLOAD 119 6. Regular Expressions and Matching . 125 Literals 125 The qr// Operator and Regex Combinations 126 Quantifiers 127 Greediness 128 Regex Anchors 129 Metacharacters 130 Character Classes 131 Capturing 132 Grouping and Alternation 133 Other Escape Sequences 135 Assertions 135 Regex Modifiers 137 Smart Matching 139 7. Objects . 141 Moose 141 Blessed References 155 Reflection 160 Advanced OO Perl 162 8. Style and Efficacy . 165 Writing Maintainable Perl 165 Writing Idiomatic Perl 166 Writing Effective Perl 167 Prepared exclusively for none ofyourbusiness Contents • vii Exceptions 168 Pragmas 171 9. Managing Real Programs . 175 Testing 175 Handling Warnings 181 Files 184 Modules 192 Distributions 196 The UNIVERSAL Package 199 Code Generation 202 Overloading 207 Taint 210 10. Perl Beyond Syntax . 213 Idioms 213 Global Variables 220 11. What to Avoid . 225 Barewords 225 Indirect Objects 228 Prototypes 230 Method-Function Equivalence 234 Automatic Dereferencing 236 Tie 237 12. Next Steps with Perl . 241 Useful Core Modules 241 What’s Next? 243 Index . 245 Prepared exclusively for none ofyourbusiness Preface Larry Wall released the first version of Perl in 1987. The language grew from its niche as a tool for system administrators who needed something more powerful than shell scripting and easier to use than C programming into a general-purpose programming language. Perl has a solid history of pragmatism and, in recent years, a disciplined approach to enhancement and backward compatibility. Over Perl’s long history—Perl 5 has been continually refined over the past twenty years—our understanding of what makes great Perl programs has changed. While you can write productive programs that never take advantage of all the language has to offer, the global Perl community has invented, bor- rowed, enhanced, and polished ideas and made them available to anyone willing to learn them. Modern Perl is a mindset. It’s an approach to writing great software with the Perl programming language. It’s how effective Perl programmers write powerful, maintainable, scalable, concise, and excellent code. It takes advantage of Perl’s extensive library of free software (the CPAN) and language features designed to multiply your productivity. You’ll benefit most from this book if you already have some experience with Perl or another programming language. If you’re comfortable writing and executing programs (and happy to consult the documentation when it’s mentioned), you’ll get the most from this book. Prepared exclusively for none ofyourbusiness report erratum • discuss Preface • x Running Modern Perl The Modern::Perl module from the CPAN (see The CPAN, on page 13) tells Perl to warn you of typos and other potential problems. It also enables new features introduced in Modern Perl releases. Unless otherwise mentioned, the code snippets in this book assume you’ve started with this basic program skeleton: #!/usr/bin/env perl use Modern::Perl '2015'; use autodie; If you don’t have Modern::Perl installed, you could write the following instead: #!/usr/bin/env perl use 5.016; # implies "use strict;" use warnings; use autodie; Some examples use testing functions such as ok(), like(), and is() (Testing on page 175). The skeleton for these examples is shown here: #!/usr/bin/env perl use Modern::Perl; use Test::More; # example code here done_testing(); At the time of writing, the current stable major Perl release is Perl 5.22. If you’re using an older version of Perl, you may not be able to run all of the examples in this book unmodified. The examples in this book work best with Perl 5.16.0 or newer, though we recommend at least Perl 5.20. While the term Modern Perl has traditionally referred to any version of Perl from 5.10.1, the language has improved dramatically over the past several years. Although Perl comes preinstalled on many operating systems, you may need to install a more modern version. Windows users, download Strawberry Perl from http://www.strawberryperl.com/ or ActivePerl from http://www.activestate.com/activeperl. Users of other operating systems with Perl already installed (and a C compiler and the other development tools), start by installing the CPAN module App::perlbrew.1 1. http://search.cpan.org/perldoc?App::perlbrew Prepared exclusively for none ofyourbusiness report erratum • discuss Credits • xi perlbrew manages multiple Perl installations, so that you can switch between versions for testing and deployment. You can also install CPAN modules in your home directory without affecting the system installation. If you’ve ever had to beg a system administrator for permission to install software, you’ll appreciate this. Credits This book would not have been possible without questions, comments, sug- gestions, advice, wisdom, and encouragement from many, many people. In particular, the author thanks this edition’s tech reviewers Andy Lester, Sean Lindsay, and Mohsen Jokar as well as Michael Swaine, editor of this edition. Contributors to this and previous editions include the following: John SJ Anderson, Peter Aronoff, Lee Aylward, Alex Balhatchet, Nitesh Bez- zala, Ævar Arnfjörð Bjarmason, Matthias Bloch, John Bokma, Géraud CONTINSOUZAS, Vasily Chekalkin, Dmitry Chestnykh, E. Choroba, Tom Christiansen, Anneli Cuss, Paulo Custodio, Steve Dickinson, Kurt Edmiston, David Farrell, Felipe, Shlomi Fish, Jeremiah Foster, Mark Fowler, John Gabriele, Nathan Glenn, Kevin Granade, Andrew Grangaard, Bruce Gray, Ask Bjørn Hansen, Tim Heaney, Graeme Hewson, Robert Hicks, Michael Hicks, Michael Hind, Mark Hindess, Yary Hluchan, Daniel Holz, Mike Huffman, Gary H. Jones II, Curtis Jewell, Mohammed Arafat Kamaal, James E Keenan, Kirk Kimmel, Graham Knop, Yuval Kogman, Jan Krynicky, Michael Lang, Jeff Lavallee, Moritz Lenz, Jean-Baptiste Mazon, Josh McAdams, Gareth McCaughan, John McNamara, Shawn M Moore, Alex Muntada, Carl Mäsak, Chris Niswander, Nelo Onyiah, Chas. Owens, ww from PerlMonks, Matt Pettis, Jess Robinson, Dave Rolsky, Gabrielle Roth, Grzegorz Rożniecki, Jean-Pierre Rupp, Eduardo Santiago, Andrew Savige, Lorne Schachter, Alex Schroeder, Steve Schulze, Dan Scott, Alex-ander Scott-Johns, Phillip Smith, Christopher E.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages278 Page
-
File Size-