What did we programming geeks get in our virtual stockings for Christmas? New versions of programming’s leading ladies, Perl and Ruby.
The first new version of Perl in five years, version 5.10 adds performance enhancements and quite a few new features. A few of my favorites include:
- A new switch statement, which doesn’t contain the token “switch” but does include powerful “smart matching”. It appears to be similar to Ruby’s “case” statement, but as you might expect in Perl, the matching rules are considerably more complicated. It reads pretty easily in usage, though. You can also invoke the smart matching algorithm in conditionals outside a switch statement using a new operator “~~”.
- A new defined-or operator. I really like the way this one works. It’s a syntactical shortcut for using “defined” along with the ternary operator. How many times have you had to code “defined $a ? $a : $b”? Now you can just say “$a // $b”. I like the operator they chose, too. It looks like a slanted “or” operator (||), and has the same precedence. Likewise, you can use “//=” to assign the result to the left operand.
- Several enhancements to regular expressions, including the ability to name capture buffers (which could get a bit wordy, but might be easier to read and maintain) and an easier syntax for writing recursive patterns.
- You can now create a lexically scoped version of $_, while still being able to access the global version.
There are a number of changes that produce incompatibilities with code written for prior Perl versions, so you’ll want to check these out thoroughly before you slap 5.10 on your public server.
And of course, this isn’t the Big Event we’re all expecting in Perl 6.
Not to be upstaged, Matz announced the release of Ruby 1.9. Wow, this version has so many new features I don’t know where to start. Many of them are marked [EXPERIMENTAL] and may or may not be included in Ruby 2.0, so use at your own risk. Quite a few introduce subtle differences that you might not notice right away, like Module#instance_methods now returns an array of symbols instead of strings, and the fact that class variables are no longer inherited.
The Regexp#=== operator now matches symbols, and Symbol#=== also matches strings. That should making testing symbols a lot easier, especially within case statements (which implicitly use the === operator). Many of the String methods have been replicated for Symbol, too.
Not sure why String is no longer an Enumerable — maybe it has to do with all the stuff they added to Enumerable. But String methods are now aware of character encoding, and converting from one encoding method to another is as simple as calling the new “force_encoding” method.
A new concurrency model called Fibers allows for limited threading of Ruby apps. Much of this feature’s details appear to be still under discussion.
Like Perl, Ruby is also anticipating a major release with version 2.0, but it looks like Matz is doing most of the shaking things up in 1.9 so that the dust can settle before 2.0.