Perl 6: Discover its new features
Mihalis Tsoukalos explains the necessary things that you need to know to start taking advantage of the unique features of Perl 6.
Perl 6 is the latest version and it supports objectoriented programming, including generics, roles and multiple dispatch, as well as functional programming primitives, including list evaluation, junctions, autothreading and hyperoperators. One major new feature is support for multi-cores, and it also supports definable grammars which increases the pattern matching capabilities of Perl, and enables developers to perform generalised string processing.
We’ll be using Rakudo, a compiler for Perl 6 code. You can install Perl 6 on an Ubuntu distro with sudo apt-get install rakudo . (The full install process is pictured, above.) Despite the name of the package, the compiler‘s executable is called perl6. Use $ perl6 -v to see the exact version, which will output something like This is perl6 version 2013.12 built on parrot 5.9.0 revision 0 . You can execute a Perl 6 file with $ perl6 file.pl . Alternatively, you can create a script with: $ cat hw.pl #!/usr/bin/env perl6 use v6; print “Hello World!\n”;
If you execute perl6 without any additional arguments or options, you’ll enter a REPL (read–eval–print loop) which is a new feature. A REPL is also a shell; a simplistic and interactive programming environment that accepts single user inputs, evaluates them and immediately returns the results to the user. It’s also handy for learning the new features of Perl 6.
New changes
If your main program file contains a subroutine called MAIN this will be automatically executed first when the program is launched. This can be helpful for getting command-line arguments and options as it gives you a CLI parser for free. The code below ( readWords.pl) demonstrates this: use v6; my $count = 0; sub MAIN($file) { print “File: $file\n”; for $file.IO.words -> $word {
$count++; } print("Total number of words in $file is $count\n");
This code also shows a new way of reading words from a file. As you can see, you no longer need to open the text file