Table of Contents

preface xi

acknowledgments xv

I Introductory elements

1 Introduction 3

   1.1   On programming 4

   1.2   On Perl 7
      Getting started 10, Running Perl 11, Getting help 12

   1.3   A bigger picture 15

2 Writing code 18

   2.1   Structure 20

   2.2   Naming 21

   2.3   Comments 24

   2.4   Being strict 29

   2.5   A quick style guide 31

3 Writing programs 32

   3.1   A first program 34
      Specification 34, Design 35, Coding 40, Testing and debugging 45, Maintenance 47

   3.2   faqgrep 49

   3.3   Exercises 55

II Essential elements

4 Data: types and variables 59

   4.1   Scalar data 60
      Scalar variables 63

   4.2   Expressions 65

   4.3   List data 67
      Array variables 69, Hash variables 71

   4.4   Context 73

   4.5   References to variables 74

   4.6   Putting it together 76

   4.7   Exercises 77

5 Control structures 78

   5.1   Selection statements 80

   5.2   Repetition: loops 84

   5.3   Logical operators 89

   5.4   Statement modifiers 92

   5.5   Putting it together 92

   5.6   Exercises 97

6 Simple I/O and text processing 98

   6.1   File handles 99

   6.2   Pattern matching 103
      Matching constructs 105, Regex language constructs 107, Matching and substitution operators 112

   6.3   Split and join 113

   6.4   The DATA file handle 114

   6.5   Putting it together 116

   6.6   Exercises 120

7 Functions 121

   7.1   Scope 123

   7.2   Global variables 127

   7.3   Parameters 127

   7.4   Return values 129

   7.5   Designing functions 131

   7.6   Parameters and references 134

   7.7   Recursion 135

   7.8   Putting it together 137
      Revisiting the mathq program 137, Routine examples 139

   7.9   Exercises 140

8 References and aggregate data structures 141

   8.1   Creating references 143
      Nested or multi-dimensional arrays 147, Nested hashes 149, Mixed structures 149

   8.2   Scope and references 150

   8.3   References to functions 152
      Closures 153

   8.4   Nested structures on the fly 155

   8.5   Review 158

   8.6   Exercises 159

9 Documentation 160

   9.1   User documentation and POD 161

   9.2   Source code documentation 164
      Other uses of LP 169

   9.3   Tangling code 170
      A simple tangler 170

   9.4   Further resources 178

III Practical elements

10 Regular expressions 183

   10.1   The basic components 184

   10.2   The character class 188
      Search and replace: capitalize headings 189, Character class shortcuts 191

   10.3   Greedy quantifiers: take what you can get 191

   10.4   Non-greedy quantifiers: take what you need 192

   10.5   Simple anchors 193

   10.6   Grouping, capturing, and backreferences 195
      Prime number regex 196

   10.7   Other anchors: lookahead and lookbehind 198
      Inserting commas in a number 198

   10.8   Exercises 201

11 Working with text 202

   11.1   The match operator 203
      Context of the match operator 206

   11.2   The substitution operator 207

   11.3   Strings within strings 208

   11.4   Translating characters 211

   11.5   Exercises 212

12 Working with lists 214

   12.1   Processing a list 215

   12.2   Filtering a list 217

   12.3   Sorting lists 217

   12.4   Chaining functions 221

   12.5   Reverse revisited 223

   12.6   Exercises 224

13 More I/O 225

   13.1   Running external commands 226

   13.2   Reading and writing from/to external commands 227

   13.3   Working with directories 228

   13.4   Filetest operators 229

   13.5   faqgrep revisited 230

   13.6   Exercises 233

14 Using modules 234

   14.1   Installing modules 236

   14.2   Using modules 237

   14.3   File::Basename 238

   14.4   Command line options 239

   14.5   The dating game 241

   14.6   Fetching webpages 243
      Stock quotes and graphs 243

   14.7   CGI.pm 249

   14.8   Reuse, don't reinvent 253

   14.9   Exercises 255

15 Debugging 256

   15.1   Debugging by hand 257

   15.2   The Perl debugger 262

IV Advanced elements

16 Modular programming 271

   16.1   Modules and packages 272

   16.2   Making a module 274

   16.3   Why make modules? 278

   16.4   Exercises 279

17 Algorithms and data structuring 280

   17.1   Searching 281

   17.2   Sorting 283

   17.3   Heap sort 286

   17.4   Exercises 291

18 Object-oriented programming and abstract data structures 292

   18.1   What is OOP? 293

   18.2   OOP in Perl 295
      The basics 295, Inheritance 299

   18.3   Abstract data structures 301

   18.4   Stacks, queues, and linked lists 301
      Stacks 301, Queues 307, Linked lists 309

   18.5   Exercises 314

19 More OOP examples 315

   19.1   The heap as an abstract data structure 316

   19.2   Grades: an object example 320

   19.3   Exercises 330

20 What's left? 331

appendix A     Command line switches 333

appendix B     Special variables 336

appendix C     Additional resources 338

appendix D     Numeric formats 340

glossary 342

index 348