Posted by brian d foy on October 13, 2010
[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 [...]
Posted by brian d foy on October 11, 2010
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 [...]
Posted by brian d foy on September 19, 2010
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 [...]
Posted by brian d foy on September 12, 2010
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 [...]
Posted by brian d foy on August 15, 2010
Perl 5.12 adds a feature that lets you locally delete a hash key or array element (refresh your memory of local with Item 43: Know the difference between my and local. This new feature allows you to temporarily prune a hash or an array: delete local $hash{$key}; delete local $array[$index]; This syntax has actually been [...]
Posted by brian d foy on June 27, 2010
Perl’s flip-flop operator, .., (otherwise known as the range operator in scalar context) is a simple way to choose a window on some data. It returns false until its lefthand side is true. Once the lefthand side is true, the flip-flop operator returns true until its righthand side is true. Once the righthand side is [...]
Posted by brian d foy on May 2, 2010
In Perl, a subroutine or other block structure that returns a value gives back the last evaluated expression, but if you’re not careful you might not recognize what that last evaluation actually is. It’s not necessarily the last statement in the block; it’s just the last one that you actually execute. For this Item, forget [...]
Posted by brian d foy on February 22, 2010
A recent question on Stackoverlow asked about the difference between the same floating numbers being stored in scientific notation and written out. Why does 0.76178 come out differently than 7.6178E-01 When Perl stores them, they can come out as slightly different numbers. This is related to the perlfaq answer to Why am I getting long [...]
Posted by brian d foy on January 31, 2010
Scopes can be confusing. Perl 5 introduced lexical, or my, variables that are only visible in the scope in which you define them. To properly scope your variables, you need to know what can define a scope and what doesn’t. You commonly see lexical variables for subroutine arguments, for instance: sub foo { my( $self, [...]
Posted by brian d foy on January 17, 2010
I recently updated perlfaq4‘s answer to “What’s the difference between a list and an array?”. The difference between data and variables is often lost of the person who starts their programming career in a high level language. We hit this subject pretty hard in the first chapter of Effective Perl Programming, 2nd Edition in at [...]