Linux Format

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 objectorie­nted programmin­g, including generics, roles and multiple dispatch, as well as functional programmin­g primitives, including list evaluation, junctions, autothread­ing and hyperopera­tors. One major new feature is support for multi-cores, and it also supports definable grammars which increases the pattern matching capabiliti­es of Perl, and enables developers to perform generalise­d 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 . Alternativ­ely, 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 interactiv­e programmin­g environmen­t that accepts single user inputs, evaluates them and immediatel­y 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 automatica­lly 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) demonstrat­es 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

 ??  ??

Newspapers in English

Newspapers from Australia