Beginning Perl Third Edition JAMES LEE with SIMON COZENS Apress
About the Author... About the Technical Reviewers Acknowledgements Suitrod yetion «. xvi xvii xviii «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
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 (<STDIN>) 67 Infinite Loops 69 Looping Until 70 The for Loop 71 vi
Theforeach Loop '"I 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
Hash in Scalar Context 120 Hash Functions 121 The keys() Function 121 The valuesq 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
Default Argument Values 150 Named Parameters 151 Summary 152 Exercises...152 Chapter 7: Regular Expressions -153 What Are They? 153 Patterns 154 Working with Regexes 170 Substitution 170 Changing Delimiters 172 Modifiers 173 The split() Function 174 The join() Function 175 Common Blunders 175 Summary 176 Exercises 177 Chapter 8: Files and Data 179 Filehandles 179 The open() Function 179 The close() Function 180 Three Ways to Open a File 181 Read Mode 182 Reading in Scalar Context 183 Reading with the Diamond 185 @ARGV: The Command-Line Arguments 187 @ARGV and <> 189 SARGV 190 Reading in List Context 190 L\
Writing to Files 192 Buffering 195 Opening Pipes 196 Receiving Piped Data from a Process 196 Sending Piped Data to Another Process 198 Bidirectional Pipes 200 File Tests 200 Summary 205 Exercises 205 Chapter 9: String Processing 207 Character Position 207 String Functions 208 The lengthtj Function 208 The index() Function 208 The rindex() Function 210 The substrf) Function 210 Transliteration 212 Summary 213 Exercises 213 Chapter 10: Interfacing to the Operating System 215 The %ENV Hash 215 Working with Files and Directories 217 File Globbing with glob() 217 Reading Directories 220 Functions to Work with Files and Directories 221 Executing External Programs 225 The system() Function 225
Backquotes 227 There's More 228 Summary 228 Exercises 229 Chapter 11: References 231 What Is a Reference? 231 Anonymity 232 The Life Cycle of a Reference 232 Reference Creation 232 Reference Modification 239 Reference Counting and Destruction 243 Using References for Complex Data Structures 244 Matrices 245 Autovivification 245 Trees 250 Summary 255 Exercises 255 Chapter 12: Modules 257 Why Do We Need Them? 257 Creating a Module 258 Including Other Files with use 260 do 260 require 261 use 262 Changing @INC 262 Package Hierarchies 263 Exporters 265
The Perl Standard Modules - 267 Online Documentation 268 Data::Dumper 268 File-Find 270 Getopt::Std 6etopt::Long 271 272 File::Spec 273 Benchmark 275 Win32 276 CPAN 278 Installing Modules with PPM 280 Installing a Module Manually 281 The CPAN Module 281 Bundles 284 Submitting Your Own Module to CPAN 285 Summary 286 Chapter 13: Object-Oriented Perl....287 00 Buzzwords 287 Objects 287 Attributes 288 Methods 288 Classes 289 Polymorphism 290 Encapsulation 290 Inheritance 290 Constructors 291 Destructors 292 An Example 292 xii
Rolling Your Own Classes 295 Bless You, My Reference 296 Storing Attributes 298 The Constructor 298 Creating Methods 301 Do You Need 00?..-313 Are Your Subroutines Tasks? 314 Do You Need Persistence? 314 Do You Need Sessions? 314 Do You Need Speed? 314 Do You Want the User to Be Unaware of the Object? 314 Are You Still Unsure? 314 Summary 315 Exercises 315 Chapter. 14: Introduction to CGI 317 We Need a Web Server, 318 Creating a CGI Directory 318 Writing CGI Programs 318 "hello, world!" in CGI 319 The CGI Environment 321 Generating HTML 323 Introducing CGI.pm 325 Conventional Style of Calling Methods 331 CGI.pm Methods 332 Methods That Generate Several Tags 332 Methods That Generate One Tag 333 Processing Form Data 333 The param() Method 335 xiii
Dynamic CGI 336 Let's Play Chess! 338 Improvements We Can Make 346 What We Did Not Talk About 347 Summary 348 Exercises 348 Chapter 15: Perl and DBI 349 Introduction to Relational Databases 349 We Need an SQL Server MySQL 353 Testing the MySQL Server 353 Creating a Database 354 Creating a Non-root User with the GRANT Command 357 The INSERT Command 358 The SELECT Command 361 Table Joins 367 Introduction to DBI 368 Installing DBI and the DBD::mysql 368 Connecting to the MySQL Database 369 Executing an SQL Query with DBI 370 A More Complex Example 372 Use Placeholders 375 DBI and Table Joins 377 Perl, DBI, and CGI 378 What We Didn't Talk About 385 Summary 386 Exercises 386 Appendix: Exercise Solutions 387 xiv
Chapter 1 387 Chapter 2 387 Chapter 3 389 Chapter 4 390 Chapter 5 391 Chapter 6 393 Chapter 7 395 Chapter 8 396 Chapter 9 398 Chapter 10 399 Chapter 11, 400 Chapter 13 404 Chapter Chapter 14 405 15 406 Index 409 XV