Effective Perl Programming

Last updated
Effective Perl Programming EffectivePerlBookCover.jpg
Effective Perl Programming

Effective Perl Programming, sometimes known as the Shiny Ball Book by Perl programmers, is an intermediate to advanced text by Joseph N. Hall covering the Perl programming language. Randal L. Schwartz contributed a foreword and technical editing.

Joseph N. Hall is an American author, software developer and programming consultant. Hall is known in the Perl programming community as the author of the book Effective Perl Programming with Randal L. Schwartz, and as a contributor of software to the CPAN.

Perl interpreted programming language

Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages, Perl 5 and Perl 6.

Programming language language designed to communicate instructions to a machine

A programming language is a formal language, which comprises a set of instructions that produce various kinds of output. Programming languages are used in computer programming to implement algorithms.

Effective Perl Programming follows the numbered "rules" format begun in Scott Meyers' Effective C++ . A small number of errors were corrected in the 2nd and 4th printings.

Scott Meyers American computer programmer

Scott Douglas Meyers is an American author and software consultant, specializing in the C++ computer programming language. He is known for his Effective C++ book series. During his career, he was a frequent speaker at conferences and trade shows.

An expanded second edition ( ISBN   0321496949), Effective Perl Programming: Ways to Write Better, More Idiomatic Perl, 2/E. by Hall, Joshua A McAdams, and brian d foy was published in 2010 by Pearson.

Here is the Table of Contents of the 2nd ed.:

  1. Chapter 1: The Basics of Perl
    • Item 1. Find the documentation for Perl and its modules.
    • Item 2. Enable new Perl features when you need them.
    • Item 3. Enable strictures to promote better coding.
    • Item 4. Understand what sigils are telling you.
    • Item 5. Know your variable namespaces.
    • Item 6. Know the difference between string and numeric comparisons.
    • Item 7. Know which values are false and test them accordingly.
    • Item 8. Understand conversions between strings and numbers.
    • Item 9. Know the difference between lists and arrays.
    • Item 10. Don’t assign undef when you want an empty array.
    • Item 11. Avoid a slice when you want an element.
    • Item 12. Understand context and how it affects operations.
    • Item 13. Use arrays or hashes to group data.
    • Item 14. Handle big numbers with bignum.
  2. Chapter 2: Idiomatic Perl
    • Item 15. Use $_ for elegance and brevity.
    • Item 16. Know Perl’s other default arguments.
    • Item 17. Know common shorthand and syntax quirks.
    • Item 18. Avoid excessive punctuation.
    • Item 19. Format lists for easy maintenance.
    • Item 20. Use foreach, map, and grep as appropriate.
    • Item 21. Know the different ways to quote strings.
    • Item 22. Learn the myriad ways of sorting.
    • Item 23. Make work easier with smart matching.
    • Item 24. Use given-when to make a switch statement.
    • Item 25. Use do {} to create inline subroutines.
    • Item 26. Use List::Util and List::MoreUtils for easy list manipulation.
    • Item 27. Use autodie to simplify error handling.
    Foreach loop

    For each is a control flow statement for traversing items in a collection. Foreach is usually used in place of a standard for statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages an iterator, even if implicit, is often used as the means of traversal.

  3. Chapter 3: Regular Expressions
    • Item 28. Know the precedence of regular expression operators.
    • Item 29. Use regular expression captures.
    • Item 30. Use more precise whitespace character classes.
    • Item 31. Use named captures to label matches.
    • Item 32. Use noncapturing parentheses when you need only grouping.
    • Item 33. Watch out for the match variables.
    • Item 34. Avoid greed when parsimony is best.
    • Item 35. Use zero-width assertions to match positions in a string.
    • Item 36. Avoid using regular expressions for simple string operations.
    • Item 37. Make regular expressions readable.
    • Item 38. Avoid unnecessary backtracking.
    • Item 39. Compile regexes only once.
    • Item 40. Pre-compile regular expressions.
    • Item 41. Benchmark your regular expressions.
    • Item 42. Don’t reinvent the regex.
  4. Chapter 4: Subroutines
    • Item 43. Understand the difference between my and local.
    • Item 44. Avoid using @_ directly unless you have to.
    • Item 45. Use wantarray to write subroutines returning lists.
    • Item 46. Pass references instead of copies.
    • Item 47. Use hashes to pass named parameters.
    • Item 48. Use prototypes to get special argument parsing.
    • Item 49. Create closures to lock in data.
    • Item 50. Create new subroutines with subroutines.
  5. Chapter 5: Files and FileHandles
    • Item 51. Don’t ignore the file test operators.
    • Item 52. Always use the three-argument open.
    • Item 53. Consider different ways of reading from a stream.
    • Item 54. Open filehandles to and from strings.
    • Item 55. Make flexible output.
    • Item 56. Use File::Spec or Path::Class to work with paths.
    • Item 57. Leave most of the data on disk to save memory.
  6. Chapter 6: References
    • Item 58. Understand references and reference syntax.
    • Item 59. Compare reference types to prototypes.
    • Item 60. Create arrays of arrays with references.
    • Item 61. Don’t confuse anonymous arrays with list literals.
    • Item 62. Build C-style structs with anonymous hashes.
    • Item 63. Be careful with circular data structures.
    • Item 64. Use map and grep to manipulate complex data structures.
  7. Chapter 7: CPAN
    • Item 65. Install CPAN modules without admin privileges.
    • Item 66. Carry a CPAN with you.
    • Item 67. Mitigate the risk of public code.
    • Item 68. Research modules before you install them.
    • Item 69. Ensure that Perl can find your modules.
    • Item 70. Contribute to CPAN.
    • Item 71. Know the commonly used modules.
  8. Chapter 8: Unicode
    • Item 72. Use Unicode in your source code.
    • Item 73. Tell Perl which encoding to use.
    • Item 74. Specify Unicode characters by code point or name.
    • Item 75. Convert octet strings to character strings.
    • Item 76. Match Unicode characters and properties.
    • Item 77. Work with graphemes instead of characters.
    • Item 78. Be careful with Unicode in your databases.
  9. Chapter 9: Distributions
    • Item 79. Use Module::Build as your distribution builder.
    • Item 80. Don’t start distributions by hand.
    • Item 81. Choose a good module name.
    • Item 82. Embed your documentation with Pod.
    • Item 83. Limit your distributions to the right platforms.
    • Item 84. Check your Pod.
    • Item 85. Inline code for other languages.
    • Item 86. Use XS for low-level interfaces and speed.
  10. Chapter 10: Testing
    • Item 87. Use prove for flexible test runs.
    • Item 88. Run tests only when they make sense.
    • Item 89. Use dependency injection to avoid special test logic.
    • Item 90. Don’t require more than you need to use in your methods.
    • Item 91. Write programs as modules for easy testing.
    • Item 92. Mock objects and interfaces to focus tests.
    • Item 93. Use SQLite to create test databases.
    • Item 94. Use Test::Class for more structured testing.
    • Item 95. Start testing at the beginning of your project.
    • Item 96. Measure your test coverage.
    • Item 97. Use CPAN Testers as your QA team.
    • Item 98. Set up a continuous build system.
  11. Chapter 11: Warnings
    • Item 99. Enable warnings to let Perl spot suspicious code.
    • Item 100. Use lexical warnings to selectively turn on or off complaints.
    • Item 101. Use die to generate exceptions.
    • Item 102. Use Carp to get stack traces.
    • Item 103. Handle exceptions properly.
    • Item 104. Track dangerous data with taint checking.
    • Item 105. Start with taint warnings for legacy code.
  12. Chapter 12: Databases
    • Item 106. Prepare your SQL statements to reuse work and save time.
    • Item 107. Use SQL placeholders for automatic value quoting.
    • Item 108. Bind return columns for faster access to data.
    • Item 109. Reuse database connections.
  13. Chapter 13: Miscellany
    • Item 110. Compile and install your own perls.
    • Item 111. Use Perl::Tidy to beautify code.
    • Item 112. Use Perl Critic.
    • Item 113. Use Log::Log4perl to record your program’s state.
    • Item 114. Know when arrays are modified in a loop.
    • Item 115. Don’t use regular expressions for comma-separated values.
    • Item 116. Use unpack to process columnar data.
    • Item 117. Use pack and unpack for data munging.
    • Item 118. Access the symbol table with typeglobs.
    • Item 119. Initialize with BEGIN; finish with END.
    • Item 120. Use Perl one-liners to create mini programs.

Related Research Articles

The Comprehensive Perl Archive Network (CPAN) is a repository of over 250,000 software modules and accompanying documentation for 39,000 distributions, written in the Perl programming language by over 12,000 contributors. CPAN can denote either the archive network itself, or the Perl program that acts as an interface to the network and as an automated software installer. Most software on CPAN is free and open source software. CPAN was conceived in 1993 and active online since October 1995. It is based on the CTAN model and began as a place to unify the structure of scattered Perl archives.

Hash table Associates data values with key values - a lookup table

In computing, a hash table is a data structure that implements an associative array abstract data type, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

Regular expression Sequence of characters that forms a search pattern

A regular expression, regex or regexp is a sequence of characters that define a search pattern. Usually this pattern is used by string searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique that developed in theoretical computer science and formal language theory.

IDL, short for Interactive Data Language, is a programming language used for data analysis. It is popular in particular areas of science, such as astronomy, atmospheric physics and medical imaging. IDL shares a common syntax with PV-Wave and originated from the same codebase, though the languages have subsequently diverged in detail. There are also two free implementations, GNU Data Language (GDL) and Fawlty Language (FL).

A Perl module is a discrete component of software for the Perl programming language. Technically, it is a particular set of conventions for using Perl's package mechanism that has become universally adopted.

ICI is a general purpose interpreted, computer programming language originally developed by Tim Long in the late 1980s. It has dynamic typing and flexible data types, with the basic syntax, flow control constructs and operators of C. It can be considered broadly similar to Perl, with which it is roughly contemporary. Like Perl, it also has tight integration with regular expressions.

Perl Data Language is a set of free software array programming extensions to the Perl programming language. PDL extends the data structures built into Perl, to include large multidimensional arrays, and adds functionality to manipulate those arrays as vector objects. It also provides tools for image processing, computer modeling of physical systems, and graphical plotting and presentation. Simple operations are automatically vectorized across complete arrays, and higher-dimensional operations are supported.

Perl 6 sixth major version of the Perl programming language

Perl 6 is a member of the Perl family of programming languages.

CGI.pm is a large and widely used Perl module for programming Common Gateway Interface (CGI) web applications, providing a consistent API for receiving and processing user input. There are also functions for producing HTML or XHTML output, but these are now unmaintained and are to be avoided. CGI.pm was a core Perl module but has been removed as of v5.22 of Perl. The module was written by Lincoln Stein and is now maintained by Lee Johnson.

Perl Compatible Regular Expressions (PCRE) is a library written in C, which implements a regular expression engine, inspired by the capabilities of the Perl programming language. Philip Hazel started writing PCRE in summer 1997. PCRE's syntax is much more powerful and flexible than either of the POSIX regular expression flavors and than that of many other regular-expression libraries.

Perl OpenGL

Perl OpenGL (POGL) is a portable, compiled wrapper library that allows OpenGL to be used in the Perl programming language.

This Comparison of programming languages compares the features of associative array data structures or array-lookup processing for over 39 various computer programming languages.

Adam Kennedy (programmer) Australian computer programmer

Adam Kennedy is an Australian Perl programmer, and one of several CPAN administrators. Under his CPAN author id of ADAMK, he is the maintainer of over 200 module distributions on CPAN, which places him at the top of the CPAN contribution leaderboard. Kennedy is the first maintainer of more than 200 CPAN modules, many of which he has adopted from other authors and included in his Open Repository, which is available for use by any registered CPAN author. He is a frequent presenter at open source conferences such as OSDC, OSCON, and YAPC as well as the Perl QA hackathons.

Plack (software) web application framework

Plack is a Perl web application programming framework inspired by Rack for Ruby and WSGI for Python, and it is the project behind the PSGI specification used by other frameworks such as Catalyst and Dancer. Plack allows for testing of Perl web applications without a live web server.

The Perl virtual machine is a stack-based process virtual machine implemented as an opcodes interpreter which runs previously compiled programs written in the Perl language. The opcodes interpreter is a part of the Perl interpreter, which also contains a compiler in one executable file, commonly /usr/bin/perl on various Unix-like systems or perl.exe on Microsoft Windows systems.

The structure of the Perl programming language encompasses both the syntactical rules of the language and the general ways in which programs are organized. Perl's design philosophy is expressed in the commonly cited motto "there's more than one way to do it". As a multi-paradigm, dynamically typed language, Perl allows a great degree of flexibility in program design. Perl also encourages modularization; this has been attributed to the component-based design structure of its Unix roots, and is responsible for the size of the CPAN archive, a community-maintained repository of more than 100,000 modules.

Mojolicious is a real-time web application framework, written by Sebastian Riedel, creator of the web application framework Catalyst. Licensed as free software under the Artistic License v 2.0, it is written in the Perl programming language, and is designed for use in both simple and complex web applications, based on Riedel's previous experience developing Catalyst. Documentation for the framework was partly funded by a grant from The Perl Foundation.

Qore is an interpreted, high-level, general-purpose, garbage collected dynamic programming language, featuring support for code embedding and sandboxing with optional strong typing and a focus on fundamental support for multithreading and SMP scalability.

The following outline is provided as an overview of and topical guide to the Perl programming language:

References