home | blog

What's new in Perl 5.10? say, //, state

Published on 2007.12.24 at 01:11:28 Bookmark and Share

Tags: Perl, Perl 5, 5.10, say


OK, so it is not news any more, Perl 5.10 has been released on 18th December, the 20th birthday of Perl. People here and there are writing articles about that, there are several presentations on-line available. For example see the discussion on PerlMonks. It has several good links.

Still I think it would be good to write a few articles about the new features. Especially as I am going to give a talk about them on the upcoming Israeli Perl Workshop in 8 days...

There are many new features, I'll have to select which ones to present. So let's start with some of the simple features.

There is a new function called say. It is the same as print but it automatically adds a new line \n to every call. This does not sound like a big issue and it is indeed not a huge one, but nevertheless it saves a lot of typing, especially in debugging code. There are just so many times we type

    print "$var\n";

Now we'll be able to say this:

    say $var;

To people worried of new functions popping up in their old code, you ain't worry. The new function is only available if you explicitly ask for it by writing

    use feature qw(say);

or if you require 5.10 to be the minimal version where your code can run:

    use 5.010;

Another cute help is the // defined-or operator. It is nearly the same as the good old || but without the "0 is not a real value" bug: Earlier when we wanted to give a default value to a scalar we could write either

    $x = defined $x ? $x : $DEFAULT;

which is quite long or we could write

    $x ||= $DEFAULT;

but then 0 or "0" or the empty string were not accepted as valid values and were replaced by the $DEFAULT value. While in some cases this is ok, in many cases this created a bug.

The new defined-or operator can solve this problem as it will return the right hand side only if the left hand value is undef. So now we are going to have short AND correct form:

    $x //= $DEFAULT;

Third thing I look at in this first article is the new state keyword. This too is optional and is only included if you ask for it by saying

    use feature qw(state);

or by

    use 5.010;

When used it is similar to my but it creates and initializes the variable only once. It is the same as the static variable in C. Earlier we had to write something like this:

    {
       my $counter = 0;
       sub next_counter {
          $counter++; 
          return $counter;
       }
    }

Which always needed lots of explanations why $counter is set to 0 only once and how can it always get you a higher number.

Now you can write this:

    sub next_counter {
       state $counter = 0;
       $counter++;
       return $counter;
    }

Which is much clearer.

blog comments powered by Disqus
Follow szabgab on Twitter
Tags
Perl (270)
Perl 5 (94)
Padre (79)
Perl 6 (42)
IDE (41)
testing (38)
CPAN (28)
business (27)
newsletter (24)
marketing (23)
training (20)
TPF (17)
open source (17)
Windows (17)
promotion (17)
Parrot (16)
YAPC (16)
Israel (15)
grants (15)
Python (14)