First Look: Perl 5.10 is a Pearl
As most Perl fans are no doubt aware, the Perl Foundation released version 5.10 last month and introduced a number of significant upgrades for the popular programming language. Perl 5.10 is the first significant feature upgrade since the 5.8 release back in 2002.
First the good news, AKA why you should go ahead and upgrade: the major new language features are turned off by default which means you can upgrade without breaking existing scripts, and take advantage of the new features for new scripts. Even cooler is ability to progressively upgrade scripts using the “use” syntax.
For instance, add the line use feature 'switch'; prior to a block of code where you’d like to take advantage of the new switch statement in Perl 5.10 and then turn it off after upgrading that block of code using the statement no feature 'switch';. New features can be enabled by name or as a collective group using the statement use feature ':5.10';.
In addition to the switch statement, there’s a new say statement which acts like print() but adds a newline character and a state feature, which enables a new class of variables with very explicit scope control.
But perhaps the most interesting of 5.10’s new features is the new ‘or’ operator, //, which is a “defined or” construct. For instance the following statements are syntactically equivalent:
$foo // $bar
defined $foo ? $foo : $bar
Obviously the first line is much more compact and (I would argue) readable — i.e. is $foo defined? If not, give it the value $bar.” You can also add an equal sign like so:
$bar //= $foo;
Which is the same as writing:
$bar = $foo unless defined $bar;
Another noteworthy new feature is the smart match operator, which the Perl Foundation explains as “a new kind of comparison, the specifics of which are contextual based on the inputs to the operator.” For example, to find if scalar $needle is in array @haystack, simply use the new ~~ operator
if ( $needle ~~ @haystack ) ...
Perl 5.10 also finally gains support for named regex statements, which means you can avoid the dreaded lines of $1 $2 etc, which often make Perl regex hard to decipher. Finally I might be able to understand what’s going on in complex regex scripts like Markdown.
Other improvements include a faster interpreter with a smaller memory footprint, better error messages and more. For full details on the new release check out the notes.
I’ll confess I abandoned Perl for Python some time ago, but after playing with 5.10 I may have to rethink that decision, Perl 5.10’s new features are definitely worth the upgrade and a must have for anyone who uses Perl on a daily basis.
See Also:

