Perl 6 in a Nutshell
Jim O'Halloran • January 18, 2004
perl linuxconfau-2004After Lunch Damian Conway addressed Linux.Conf.Au presenting "Perl 6 in a nutshell". Essentially, although Perl 5 is great, the source code is becoming very difficult to work with, and the language syntax is coming convoluted at times. So Perl 6 was created to learn from past mistakes, and apply hindsight to the design of Perl. In many areas the language and syntax needed a rethink.
Perl 6 runs on a new platform called Parrot, which is a new virtual machine for Perl, guaranteed to be faster than the Perl 5 interpreter. Although its currently 10 times faster than Perl 5, they expect it to slow down as they add more language featured before release, but overall the target is for at least 2 times faster. Parrot will also run Perl 5, Java and Python, and interconnect the languages. They also want Python to run faster on Parrot than it does natively. Also, because Perl 5 will run on Parrot, and can be called by Perl 6 code everything on CPAN will still work under Perl 6.
Some language differences in Perl 6. The dereferencing operator -> becomes a . to fit more closely with other languages.
There are also new syntax rules for sigils (the $ (scalar), % (hash), or @ ( array) before variable names). Now, no matter what the context, you always use the same sigil with hashes and arrays. Previously accessing an element of a hash was done with $hash{index}, and the hash itself referred to as %hash, this becomes %hash{index} and %hash, in Perl 6, much simpler.
Perl 6 now supports Unicode. Both source code and data are assumed to be Unicode by default, but ASCII is still supported where required.
There is a new data type called a junction which is a single scalar that acts like several scalar values. This sounds weird but it allows you to write some really simple code such as…
if (any(@numbers) > 10) { … } #True if any of the numbers in the array are less than 10.
if (one(@roots) != 0) {…} #True if one of @roots is not zero.
Perl 5 also supports multiple comparisons, such as:
if (1 < $x < 10) { } #True if $x is between 1 and 10.
There is now a ~~ or "generic smart match" operator. Given two values will try to find the most intelegent way to compare them, This means that you no longer have to remember to use == for numbers and eg for strings. This is also a lot like VB's type coersion features.
There is now no need to break indentation on here documents. Heredocs can be indented in the code without indenting the document contents.
Subroutines can now have parameter lists (finally), and parameters can be strongly typed.
Perl 6 also has a switch statement. Syntax is as follows:
Given $val {
When something { }
When soimethingelse {}
Default {}
}
For loop syntax is now also modified to be just "for LIST BLOCK" so ...
For @array -> $value { … }
.. is now valid syntax. Not to mention fairly easy to read.
Finally in Perl 6 classes are declared explicitly "class DogTag { … }" and may now have proper constructors and destructors.
All in all Perl 6 looks to be a significant improvement to Perl 5, but it does still retain its line noise look from Perl 5. The main problem is that some of the bits of noise now mean different things than they did in Perl 5, which is a little off putting. Its really inspired me to take a look at Python instead.