Perl 6 for better code

From AJS.COM

Jump to: navigation, search

Perl 6 is a powerful programming language, derived loosely from Perl versions 5 and before. Will it make your code better? That depends on you, but if you want it to, my strong belief is that it will!

How so?

Explicitness

Perl is not a language known for its explicitness, to say the least. And yet, Perl 6 gives you the tools to be as explicit as you like, and in fact, much moreso than in most languages.

  • You can name the types of your variables, asserting correct behavior when they are assigned to.
  • You (the caller) can name parameters to any function, allowing clear naming of each input.
  • You (the function author) can name and type parameters, requiring named passing where useful.
  • A very high-level switch-like construct allows code abstration to be much more regular.
  • More powerful looping constructs allow more readable code.
  • Subroutines and methods can easily be scoped, allowing more explicit definition of local utilities.

Naming things

Along the same lines as naming parameters, other things can be named in Perl 6. Here's an example of naming values in regular expressions:

 my Str $year;
 my Str $month;
 my Str $day;
 regex { $year:=(\d**{4}) - $month:=(\d**{2}) - $day:=(\d**{2}) } ~~ $date;
 say("It is the year $year, month $month and day $day") if $year.defined;

Rather than the Perl 5:

 my($year,$month,$day) = ($date =~ m{ (\d{4}) - (\d{2}) - (\d{2}) }x);
 print "It is the year $year, month $month and day $day\n" if defined $year;

Which cannot be generalized to

Personal tools