Effective Perl programming: ways to write better, more idiomatic Perl [2nd ed] 9780321496942, 0321496949

The Classic Guide to Solving Real-World Problems with Perl--Now Fully Updated for Today's Best Idioms!For years, ex

275 97 3MB

English Pages xix, 473 pages: illustrations [500] Year 2010

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Cover......Page 1
Contents......Page 8
Foreword......Page 12
Preface......Page 14
Acknowledgments......Page 18
About the Authors......Page 20
Introduction......Page 22
Item 1. Find the documentation for Perl and its modules.......Page 30
Item 2. Enable new Perl features when you need them.......Page 33
Item 3. Enable strictures to promote better coding.......Page 35
Item 4. Understand what sigils are telling you.......Page 38
Item 5. Know your variable namespaces.......Page 40
Item 6. Know the difference between string and numeric comparisons.......Page 42
Item 7. Know which values are false and test them accordingly.......Page 44
Item 8. Understand conversions between strings and numbers.......Page 48
Item 9. Know the difference between lists and arrays.......Page 52
Item 10. Don’t assign undef when you want an empty array.......Page 55
Item 11. Avoid a slice when you want an element.......Page 58
Item 12. Understand context and how it affects operations.......Page 62
Item 13. Use arrays or hashes to group data.......Page 66
Item 14. Handle big numbers with bignum.......Page 68
Chapter 2 Idiomatic Perl......Page 72
Item 15. Use $_ for elegance and brevity.......Page 74
Item 16. Know Perl’s other default arguments.......Page 77
Item 17. Know common shorthand and syntax quirks.......Page 81
Item 18. Avoid excessive punctuation.......Page 87
Item 19. Format lists for easy maintenance.......Page 89
Item 20. Use foreach, map, and grep as appropriate.......Page 91
Item 21. Know the different ways to quote strings.......Page 94
Item 22. Learn the myriad ways of sorting.......Page 98
Item 23. Make work easier with smart matching.......Page 105
Item 24. Use given-when to make a switch statement.......Page 107
Item 25. Use do {} to create inline subroutines.......Page 111
Item 26. Use List::Util and List::MoreUtils for easy list manipulation.......Page 113
Item 27. Use autodie to simplify error handling.......Page 117
Item 28. Know the precedence of regular expression operators.......Page 120
Item 29. Use regular expression captures.......Page 124
Item 30. Use more precise whitespace character classes.......Page 131
Item 31. Use named captures to label matches.......Page 135
Item 32. Use noncapturing parentheses when you need only grouping.......Page 137
Item 33. Watch out for the match variables.......Page 138
Item 34. Avoid greed when parsimony is best.......Page 140
Item 35. Use zero-width assertions to match positions in a string.......Page 142
Item 36. Avoid using regular expressions for simple string operations.......Page 146
Item 37. Make regular expressions readable.......Page 150
Item 38. Avoid unnecessary backtracking.......Page 153
Item 39. Compile regexes only once.......Page 158
Item 40. Pre-compile regular expressions.......Page 159
Item 41. Benchmark your regular expressions.......Page 160
Item 42. Don’t reinvent the regex.......Page 163
Item 43. Understand the difference between my and local.......Page 166
Item 44. Avoid using @_ directly unless you have to.......Page 175
Item 45. Use wantarray to write subroutines returning lists.......Page 178
Item 46. Pass references instead of copies.......Page 181
Item 47. Use hashes to pass named parameters.......Page 185
Item 48. Use prototypes to get special argument parsing.......Page 189
Item 49. Create closures to lock in data.......Page 192
Item 50. Create new subroutines with subroutines.......Page 197
Item 51. Don’t ignore the file test operators.......Page 200
Item 52. Always use the three-argument open.......Page 203
Item 53. Consider different ways of reading from a stream.......Page 204
Item 54. Open filehandles to and from strings.......Page 207
Item 55. Make flexible output.......Page 210
Item 56. Use File::Spec or Path::Class to work with paths.......Page 213
Item 57. Leave most of the data on disk to save memory.......Page 216
Item 58. Understand references and reference syntax.......Page 222
Item 59. Compare reference types to prototypes.......Page 230
Item 60. Create arrays of arrays with references.......Page 232
Item 61. Don’t confuse anonymous arrays with list literals.......Page 235
Item 62. Build C-style structs with anonymous hashes.......Page 237
Item 63. Be careful with circular data structures.......Page 239
Item 64. Use map and grep to manipulate complex data structures.......Page 242
Chapter 7 CPAN......Page 248
Item 65. Install CPAN modules without admin privileges.......Page 249
Item 66. Carry a CPAN with you.......Page 252
Item 67. Mitigate the risk of public code.......Page 256
Item 68. Research modules before you install them.......Page 260
Item 69. Ensure that Perl can find your modules.......Page 263
Item 70. Contribute to CPAN.......Page 267
Item 71. Know the commonly used modules.......Page 271
Chapter 8 Unicode......Page 274
Item 72. Use Unicode in your source code.......Page 275
Item 73. Tell Perl which encoding to use.......Page 278
Item 74. Specify Unicode characters by code point or name.......Page 279
Item 75. Convert octet strings to character strings.......Page 282
Item 76. Match Unicode characters and properties.......Page 286
Item 77. Work with graphemes instead of characters.......Page 290
Item 78. Be careful with Unicode in your databases.......Page 293
Item 79. Use Module::Build as your distribution builder.......Page 296
Item 80. Don’t start distributions by hand.......Page 299
Item 81. Choose a good module name.......Page 304
Item 82. Embed your documentation with Pod.......Page 308
Item 83. Limit your distributions to the right platforms.......Page 313
Item 84. Check your Pod.......Page 316
Item 85. Inline code for other languages.......Page 319
Item 86. Use XS for low-level interfaces and speed.......Page 322
Chapter 10 Testing......Page 328
Item 87. Use prove for flexible test runs.......Page 329
Item 88. Run tests only when they make sense.......Page 332
Item 89. Use dependency injection to avoid special test logic.......Page 335
Item 90. Don’t require more than you need to use in your methods.......Page 338
Item 91. Write programs as modulinos for easy testing.......Page 341
Item 92. Mock objects and interfaces to focus tests.......Page 345
Item 93. Use SQLite to create test databases.......Page 351
Item 94. Use Test::Class for more structured testing.......Page 353
Item 95. Start testing at the beginning of your project.......Page 356
Item 96. Measure your test coverage.......Page 363
Item 97. Use CPAN Testers as your QA team.......Page 367
Item 98. Set up a continuous build system.......Page 369
Chapter 11 Warnings......Page 378
Item 99. Enable warnings to let Perl spot suspicious code.......Page 379
Item 100. Use lexical warnings to selectively turn on or off complaints.......Page 382
Item 101. Use die to generate exceptions.......Page 385
Item 102. Use Carp to get stack traces.......Page 387
Item 103. Handle exceptions properly.......Page 391
Item 104. Track dangerous data with taint checking.......Page 393
Item 105. Start with taint warnings for legacy code.......Page 396
Item 106. Prepare your SQL statements to reuse work and save time.......Page 398
Item 107. Use SQL placeholders for automatic value quoting.......Page 403
Item 108. Bind return columns for faster access to data.......Page 405
Item 109. Reuse database connections.......Page 407
Item 110. Compile and install your own perls.......Page 412
Item 111. Use Perl::Tidy to beautify code.......Page 415
Item 112. Use Perl Critic.......Page 419
Item 113. Use Log::Log4perl to record your program’s state.......Page 424
Item 114. Know when arrays are modified in a loop.......Page 431
Item 115. Don’t use regular expressions for comma-separated values.......Page 433
Item 116. Use unpack to process columnar data.......Page 435
Item 117. Use pack and unpack for data munging.......Page 437
Item 118. Access the symbol table with typeglobs.......Page 444
Item 119. Initialize with BEGIN; finish with END.......Page 446
Item 120. Use Perl one-liners to create mini programs.......Page 449
Books......Page 456
Appendix B: Map from First to Second Edition......Page 460
Websites......Page 457
Getting Help......Page 458
Index......Page 466
A......Page 467
B......Page 468
C......Page 469
D......Page 472
E......Page 474
F......Page 475
G......Page 476
I......Page 477
L......Page 478
M......Page 480
N......Page 481
P......Page 482
R......Page 485
S......Page 487
T......Page 490
U......Page 492
W......Page 493
Z......Page 494

Effective Perl programming: ways to write better, more idiomatic Perl [2nd ed]
 9780321496942, 0321496949

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers