Looking toward Perl 5.10

I haven’t been paying a lot of attention to the development of core Perl lately, but a few headlines caught my attention this week and I decided to do some catching up. Per Perl Buzz, Perl 5.10’s first release candidate is coming soon. This will be the first feature release of Perl since 5.8 in 2002 and there are several interesting new features worth mentioning.

What did you say?

I’ll start with something simple but useful: the new built-in command say. It operates the same as print, but adds a newline to the end of the string printed. My pinky finger will enjoy that added convenience.

Not just a case statement

I’ve never been one to complain about Perl’s lack of a proper case statement, though I can understand why it might be nice to have. Let’s take a look:

my @in_here = qw(one two three);
my $blah = 'blather';
given ($blah) {
    when ('blather') { say "it equals blather" }
    when (/something/) { say "it matches 'something'" }
    when (/^d$/ && $_ < 100) { say "it is less than 100" }
    when (@in_here) { say "it is in the array" }
    default { "Didn't match anything" }
}

In typical Perl fashion, the code reads nicely but is a bit different from other languages. We have given, which kicks off the case statement and identifies what we’ll be switching on. The word when declares each case and default is an optional construct to handle the case when no others match. The most interesting part, though, is the way it tries to do what you want when matching each individual case. It does this using the another interesting new operator called the smart match.

Smart matching

Smart matching is perhaps the most innovative new feature in Perl 5.10. It is a new operator that can be used to inteligently compare two arguments of nearly any type. Take a look at a few examples:

$a ~~ $b; # same as $a eq $b or $a == $b
          # depending on whether one is a number
%a ~~ %b; # are keys identical?
@a ~~ @b; # do arrays contain same values in same order?
@a ~~ %b; # hash value slice truth
⊂ ~~ $a # truth of ⊂($a)

See the full table if you’re curious about the rules it uses to run its comparisons. I’m a bit concerned that this new feature tries to be too smart, but I’ll wait a while to pass final judgement.

Defined vs. truth

Back to something a bit simpler, there is a shiny, new “or” operator for Perl users to flaunt. It is very similar to the existing || operator but concerns itself with definededness instead of truth value. For example:

$a = 0;
$b = 'blah';
$c = undef;

$a || $b # 'blah'
$a // $b # 0
$c || $b # 'blah'
$c // $b # 'blah'

This looks like a very useful shortcut and I think will help clear up some of the confusion around what is considered true/false or defined/undefined. Again, I will probably find myself wishing I could use this in my perl 5.8 scripts.

That’s not all!

For a more comprehensive list of new features in Perl 5.10, I recommend Ricardo Signes’ presentation: Perl 5.10 for people who aren’t totally insane. Until next time…

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*