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”
Author: brian d foy
Set default values with the defined-or operator.
[This is a mid-week bonus Item since it’s so short]
Prior to Perl 5.10, you had to be a bit careful checking a Perl variable before you set a default value. An uninitialized value and a defined but false value both acted the same in the logical ||
short-circuit operator. The Perl idiom to set a default value looks like this: Continue reading “Set default values with the defined-or operator.”
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 return value of given
Perl 5.14, when it’s released, allows you to use a return value from given-when
. You have to wrap it in a do
(Item 25: Use do {} to create inline subroutines): Continue reading “Use the return value of given”
Use rational numbers for arbitrary precision
This Item was suggested by Shawn Corey as part of our Free eBook give-away. He’s our September winner! Continue reading “Use rational numbers for arbitrary precision”
In Perl v5.12, length(undef) returns undef.
Starting with v5.12, the length of an undefined value is undefined itself. Instead of converting the undef to the empty string then taking the length of that string, v5.12 returns undef: Continue reading “In Perl v5.12, length(undef) returns undef.”
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”