Avoid modifying scalars connected to string filehandles

Since Perl 5.8, you can treat a string as a file (Item 54. Open filehandles to and from strings). You can open a filehandle, read from the string, write to the string, and most of the other things that you can do with a file. There are some gotchas though, when you deal with that string as a normal string and a filehandle at the same time. We’ve filed this as RT 78980: Odd behavior when string filehandles and scalar assignment collide. Continue reading “Avoid modifying scalars connected to string filehandles”

Use array references with the array operators

[Update: Perl v5.24 removes this experimental feature]

There’s a significant change in syntax showing up in Perl 5.14. The array operators push, pop, shift, and unshift previously only worked on named arrays or dereferenced references. Now, thanks to David Golden, they’ll work on array references. Not only that, they’ll work on references that you’ve stored in variables or that come as the return values from subroutine calls. This feature will show up in Perl 5.13.7, so you need to compile a development version to try this: Continue reading “Use array references with the array operators”

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”

Use the return value from srand

[This is another bonus, mid-week item since it’s so short and probably mostly useless as a tweak to what you already do.]

Perl 5.14 changes srand to return the seed that it used to start the pseudorandom number generator that gives you numbers through rand. There are plenty of interwebs that will explain the difference between real randomness and the sort that you get from computers, but for this item, suffice it to say that the numbers you get from perl are completely deterministic. If you start with the same seed, you get the same sequence. Continue reading “Use the return value from srand”

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”

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.”