Compile a development version of perl

Nowadays, perl development happens at a fast clip. Every month there’s a new development release that gives you a preview of what’s going to show up in the next stable version. This not only gives the perl developers a chance to test the new perl in the wild, but also for you to try new features so you can get your application in shape for the next stable release of perl. As The Effective Perl gives you a preview of some of the upcoming features, you’ll need a development version of perl to try them for yourself. Continue reading “Compile a development version of perl”

Let perl create your regex stringification

Perl 5.14 changes how regular expression objects stringify. This might not seem like a big deal at first, but it exposes a certain sort of bug that you may have never considered. It even broke several modules on CPAN. If you previously tested for hard-coded stringifications of patterns, Perl 5.14 is probably going to break your code. Continue reading “Let perl create your regex stringification”

Use bitfields to index and search data

Although Perl makes it very easy to create, extend, or otherwise modify arrays, that doesn’t mean that a Perl array is the best way to store and search data. Not only do large arrays use up a lot of extra memory for each element (for an in-depth discussion, see the “Tie” chapter in Mastering Perl), but you don’t want to repeatedly traverse many arrays looking for what you’re after. Continue reading “Use bitfields to index and search data”

Specify any character by its octal ordinal value.

Perl 5.14 gives you some new ways to represent characters so you can avoid some annoying and ambiguous interpolations. Not only that, the new syntax unifies the different ordinal representations so you can specify characters using the same syntax even if you want to use different bases. This feature was added in Perl 5.13.3, in the development branch leading to the next stable version. Continue reading “Specify any character by its octal ordinal value.”

Use the /r substitution flag to work on a copy

How many times has this happened to you? You want to modify each element of an array so you send it through a map (Item 20. Use foreach, map, and grep as appropriate.). However, instead of your expected output, you only to get a bunch of numbers or empty strings back? For example, in this case, some digits got into the names of the cats and you want to remove them with a substitution: Continue reading “Use the /r substitution flag to work on a copy”

Understand “global” variables.

Perl has two sorts of variables: the lexical variables that are limited to a particular scope, and package variables that you define in a namespace. The package variables are sometimes also called global variables because they are visible from anywhere in the program as long as you know their name. As you might suspect, Perl makes it a bit more interesting: there are many sorts of global variables. Continue reading “Understand “global” variables.”

Use branch reset grouping to number captures in alternations

Perl’s regular expressions have a simple rule for capturing groups. It counts the order of left parentheses to assign capture variables. Not all capture groups must actually match parts of the string, and Perl doesn’t care if they do. Perl assigns capture groups inside an alternation consecutively, even though it knows that only one branch of the alternation will match. Perl 5.10 adds the branch reset, (?|alternation) which mitigates that, though. Continue reading “Use branch reset grouping to number captures in alternations”

Use when() as a statement modifier

Perl 5.10 introduced the given-when statement, and Perl 5.12 refines it slightly by letting you use the when as a statement modifier. A statement modifier puts the conditional expression at the end of the statement (see perlsyn). You’ve probably already used many of these: Continue reading “Use when() as a statement modifier”