Modern Perl 2011-2012 Edition
Total Page:16
File Type:pdf, Size:1020Kb
Modern Perl 2011-2012 edition chromatic Modern Perl 2011-2012 edition Copyright © 2010-2012 chromatic Editor: Shane Warden Logo design: Devin Muldoon Cover design: Allison Randal, chromatic, and Jeffrey Martin ISBN-10: 0-9779201-7-8 ISBN-13: 978-0-9779201-7-4 Published by Onyx Neon Press, http://www.onyxneon.com/. The Onyx Neon logo is a trademark of Onyx Neon, Inc. Onyx Neon typesets books with free software, especially Ubuntu GNU/Linux, Perl 5, PseudoPod, and LATEX. Many thanks to the contributors who make these and other projects possible. 2010 - 2011 Edition October 2010 2011 - 2012 Edition January 2012 Electronic versions of this book are available from http://onyxneon.com/books/modern_perl/, and the companion website is http://modernperlbooks.com/. Please share with your friends and colleagues. Thanks for reading! Contents Running Modern Perl . .i Credits . ii Preface i 1 The Perl Philosophy 1 Perldoc . .1 Expressivity . .2 Context ..........................................................3 Void, Scalar, and List Context . .4 Numeric, String, and Boolean Context . .5 Implicit Ideas . .5 The Default Scalar Variable . .5 The Default Array Variables . .7 2 Perl and Its Community 9 TheCPAN.........................................................9 CPAN Management Tools . 10 Community Sites . 11 Development Sites . 11 Events........................................................... 11 IRC ............................................................ 12 3 The Perl Language 13 Names........................................................... 13 Variable Names and Sigils . 14 Namespaces . 14 Variables . 15 Variable Scopes . 15 Variable Sigils . 15 Anonymous Variables . 16 Variables, Types, and Coercion . 16 Values........................................................... 16 Strings . 16 Unicode and Strings . 18 Character Encodings . 19 Unicode in Your Filehandles . 19 Unicode in Your Data . 20 Unicode in Your Programs . 20 Implicit Conversion . 20 Numbers . 22 Undef........................................................ 22 The Empty List . 23 Lists......................................................... 23 Control Flow . 24 Branching Directives . 24 The Ternary Conditional Operator . 27 Short Circuiting . 27 Context for Conditional Directives . 28 Looping Directives . 28 Iteration and Aliasing . 30 Iteration and Scoping . 30 The C-Style For Loop . 31 While and Until . 32 Loops within Loops . 34 Loop Control . 35 Continue . 36 Given/When . 36 Tailcalls . 38 Scalars . 39 Scalars and Types . 39 Arrays........................................................... 40 Array Elements . 40 Array Assignment . 41 Array Operations . 42 Array Slices . 43 Arrays and Context . 44 Array Interpolation . 44 Hashes........................................................... 45 Declaring Hashes . 45 Hash Indexing . 46 Hash Key Existence . 47 Accessing Hash Keys and Values . 48 Hash Slices . 48 The Empty Hash . 49 Hash Idioms . 50 Locking Hashes . 51 Coercion . 51 Boolean Coercion . 52 String Coercion . 52 Numeric Coercion . 52 Reference Coercion . 52 Cached Coercions . ..