Perl Programming/Perl 5.10 additions

= Perl 5.10 =

Perl 5.10 was a release version of the perl programming language that included a few new special features. Around the same time that Perl 5.10 was being prepared, design was moving forward rapidly in the design of the new Perl 6 language. Perl 6 was going to be very different from Perl 5, but in some ways still very much the same.

Because the two versions were being designed simultaneously and because some of the same people were involved in both design processes, the two share a few features. From the exchange, Perl 5.10 got the smart-match operator, a switch-style given-when control structure, a defined-or operator and state variables, plus a number of internal performance enhancements.

Smart match operator
The smart match operator is like == and eq and =~ all crammed together, with a few extra features added as well. Smart match determines whether two objects match, regardless of what type of objects you throw at it.

Given - when
Perl finally has a switch-like statement built into the language. Instead of using the popular switch and case keywords found in C, Perl 5.10 uses given and when.

For instance:

Perl's <tt>given</tt>/<tt>when</tt> construct is much more powerful than C's <tt>switch</tt>/<tt>case</tt> construct, however. Rather than just allowing integers as the match criteria for each <tt>when</tt> clause, strings, regular expressions, and even arrays are allowed. For instance:

Defined-or
Consider the oft-used code fragment:

or, the more idiomatically-Perl version:

Since this is so common, it was determined to make it into its own operator: the defined-or operator:

State variables
Perl 5.10 introduced the state keyword to declare persistent variables that retain their values between calls to a subroutine. State variables serve much the same purpose as static variables in C.

With previous versions of Perl, to declare a persistent variable, we had to declare it outside the subroutine:

This construct is rather ugly, because the subroutine must be placed within a block itself. Using the new <tt>state</tt> keyword, we can rewrite the code above as:

Resources

 * Perl 5.10 release announcement: http://dev.perl.org/perl5/news/2007/perl-5.10.0.html