Moving From Perl 5 to Perl 6 – What's New, Tutorial Style

Posted by Artem Russakovskii on August 22nd, 2008 in Programming

Updated: August 28th, 2008

imageNewsflash: Perl 6 is not dead (in case you thought it was)!

I stumbled upon this most excellent series of posts by Moritz Lenz of perlgeek.de that describe the differences between Perl 5 and the upcoming Perl 6 (thanks to Andy Lester for the link). The posts are done in the form of tutorials, which helps comprehension. Simply awesome, Moritz.

It seems like Perl 6 is going to be a lot more object oriented, but such orientation is optional and not forced upon programmers, like in, say, Java. It warms my heart that I will be able to do this (you did see the new "say" function in Perl 5.10, right?):

my Num $x = 3.4;
say $x.WHAT; # Num
say "foo".WHAT; # Str

My favorite Perl 6 change so far is this:

# named arguments
sub doit(:$when, :$what) {
  say "doing $what at $when";
doit(what => 'stuff', when => 'once');  # 'doing stuff at once'
doit(:when<noon>, :what('more stuff')); # 'doing more stuff at noon'

I've first seen this technique in Ruby (apparently Python has it too), and have been using an anonymous hash in order to emulate named arguments in Perl 5. Perl 6 does it in a much cleaner way.

I wonder if there are any Perl 6 changes specifically affecting file/disk access, MySQL interaction, and execution speed.

What is your favorite new feature? Comments welcome.

Edit: Whoa, string concatenation is now ~, the dot . is used for method calls. That's kind of upsetting, I'm so used to '.'.

Edit #2: Holy crap, regex changed so much, it just warped my head onto itself and now I have a black hole in place of my face, thanks a lot. Regexes are also now called "Rules". More here

● ● ●
Artem Russakovskii is a San Francisco programmer and blogger. Follow Artem on Twitter (@ArtemR) or subscribe to the RSS feed.

In the meantime, if you found this article useful, feel free to buy me a cup of coffee below.

  • doit(:when, :what('more stuff')); # 'doing more stuff at noon'

    Where did "noon" come from?

  • Sorry, Baron, wordpress ate my <>. It's corrected now.

  • Dave Doyle

    Agreed. This is an excellent series of posts that really brings the Perl 5 programmer into Perl 6. Heck of a lot easier to understand than the Apocalysis and Egesis stuff.

  • > I wonder if there are any Perl 6 changes
    > specifically affecting file/disk access, MySQL
    > interaction, and execution speed.

    I don't really know about disk access, but in terms of speed there are many improvements compared to perl 5. The two perhaps most important things are:
    1) Static types allow better optimiztions
    2) Overloading is gone (and replaced by multi method dispatch), and overloading was one of the perl 5 features that prevented many optimizations.

    There's no mysql specific features in Perl 6, but the new object model (about which I wrote about today, and which has many more features than I could show) will certainly help to build an easier, cleaner interface.

  • found your page and thought it was about beer… now i'm a little bit disappointed 😉

  • Jay, allow me to refer you to http://beerpla.net/2006/06/02/beer-planet/

  • Hehe i like you beerpla.net. Sounds like Bierplanet. Still funny.

  • Ya I was really like my god how do they expect me to use ~ for concatenation I cannot type that character that quickly! Well I only use that for appending to a string:

    my Str $str;
    $str ~= 'asdf';

    For concatenating several strings though fortunately there is the join method:

    ["Hello", " ", "World"].join.say;

  • femi fagbemi taiwo

    this perl 6 of a thing is gonna be a complicating idea cos i already can see myself disliking perl cos of this perl 6 thing

  • the same exactly I'm thinking.. ?? is `:when` coverted to `noon` ?