Reader small image

You're reading from  Perl 6 Deep Dive

Product typeBook
Published inSep 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787282049
Edition1st Edition
Languages
Right arrow
Author (1)
Andrew Shitov
Andrew Shitov
author image
Andrew Shitov

Andrew Shitov has been a Perl enthusiast since the end of the 1990s, and is the organizer of over 30 Perl conferences in eight countries. He worked as a developer and CTO in leading web-development companies, such as Art. Lebedev Studio, Booking dotCom, and eBay, and he learned from the "Fathers of the Russian Internet", Artemy Lebedev and Anton Nossik. Andrew has been following the Perl 6 development since its beginning in 2000. He ran a blog dedicated to the language, published a series of articles in the Pragmatic Perl magazine, and gives talks about Perl 6 at various Perl events. In 2017, he published the Perl 6 at a Glance book by DeepText, which was the first book on Perl 6 published after the first stable release of the language specification.
Read more about Andrew Shitov

Right arrow

Differences from Perl 5

Let's briefly look at some of the changes that happened on the way to Perl 6. In the following sections, you will see a few examples of code in Perl 5 and 6. They are intended for a general understanding of how Perl 5 transforms to Perl 6, but you are not expected to understand every bit of it. All the details about the syntax of Perl 6 will be explained later in this book.

Sigils

One of the most difficult things for the newcomers to Perl are sigils. A sigil is a character in front of the name of variables in Perl that denotes the structural type of a variable. For example, $ for scalar values, @ for arrays, and % for hashes.

The problem arises when you access an element of an array or hash. Let's consider the following lines of code as an example in Perl 5 of an array with the first few Fibonacci numbers:

my @fibonacci = (0, 1, 1, 2, 3, 5, 8, 13);
print $fibonacci[4];

First, a @fibonacci array is created. The name of the variable includes the @ character as a sigil. In the second line, we access one of the elements of that array and use another sigil, $, this time. This is because a single element of an array is a scalar, and scalars use $ as a sigil. For those who learn Perl, this small change in sigils is a big problem in understanding the basics of the language.

In Perl 6, sigils are unified and are part of the variable names. You do not change it independently, whether you access an element of an array or an array as a whole. The preceding example will look like this in Perl 6:

my @fibonacci = (0, 1, 1, 2, 3, 5, 8, 13);
print @fibonacci[4];

In both lines, the same sigil is used for the @fibonacci array and for its @fibonacci[4] element. This approach is much more consistent and easier for a beginner.

Signatures

In Perl 5, you had to extract the values of the arguments of a function yourself by using either the built-in shift function, or from the default @_ array.

Let's see this in the following example, with a function to calculate the sum of its two arguments. In Perl 5, you had to do some additional work to get the actual passed parameters.

First, get the argument values with shift in Perl 5:

sub add {
my $x = shift;
my $y = shift;
return $x + $y;
}

Then, by using the @_ array:

sub add {
my ($x, $y) = @_;
return $x + $y;
}

Unlike many other programming languages, it was not possible to declare a list of the function's formal parameters directly. For instance, this is how you do it in C or C++:

int add(int x, int y) {
    return x + y;
}

In Perl 5, it is possible to restrict the number of arguments and their structural types with the help of prototypes. Sigils are used there to tell Perl the type of the argument. The preceding function for addition may look like this in Perl 5:

sub add($$) {
    my ($x, $y) = @_;
    return $x + $y;
}

Using function prototypes will make the compiler complain when the function is used with the different number of arguments (say, one or three instead of two), but you still have to get their values yourself.

Perl 5.20 introduced function signatures. So, now, you may benefit from declaring the arguments in one go. The following code gives an example of such approach. Both $x and $y arguments are declared in the function header.

use v5.20;

use feature qw(signatures);
no warnings qw(experimental::signatures);

sub add($x, $y) {
    return $x + $y;
}

say add(4,5);

You will notice that you need to instruct Perl to use the features of Perl 5.20 by mentioning the minimal version number in the script. You will also notice that you must activate the corresponding feature by a separate instruction. However, even more, because signatures are an experimental feature, you have to manually disable the warning message to get a clean output.

In Perl 6, function signatures are allowed from the very beginning, so you may directly use it:

# This is Perl 6
sub add($x, $y) {
    return $x + $y;
}

Actually, signatures in Perl 5.20 are an example of backporting features from Perl 6 to Perl 5. So, despite the fact that Perl 6 was meant to be the next version of Perl 5, Perl 5 still gets some elements that were designed in Perl 6 to make Perl better.

Classes

To make the user experience better, let's take a look at another important example of where the syntax of Perl changes in Perl 6.

Traditionally, object-oriented programming is done in Perl 5 with the help of the so-called blessed hashes. Data members of an object are elements of the hash, and the blessed reference to that hash may be used to call a method on an instance of the class. The following example shows you what to do to define a class and create an instance of it in Perl 5:

package MyClass;

sub new {
    my ($class) = @_;
    my $this = {
        counter => 0
    };
    bless $this, $class;
    return $this;
}

sub inc {
    my ($this) = @_;
    $this->{counter}++;
    return $this->{counter};
}

1;

So far, the class named MyClass has two methods—new, to create a new instance, and inc, to increment the counter and return the new value. When dealing with Perl 5's classes, don't forget to return a true value at the end of the module, and that is the goal of the 1 in the last line of the file.

In the main program, you can use MyClass by creating an instance and calling methods on the variable as follows:

use MyClass;

my $var = MyClass->new;

print $var->inc, "\n";
print $var->inc, "\n";
print $var->inc, "\n";

The implementation of the object-oriented things in Perl 5 was another obstacle for newcomers who may have had an experience of working with classes in other languages but were confused by the way Perl 5 created them.

Classes in Perl 6 are way more familiar to developers who have worked with other object-oriented programming languages.

This is how you define the same class, as shown in the preceding example, in Perl 6:

class MyClass {
    has $!counter;      

    method inc() {
        $!counter++;
        return $!counter;
    }
}

As you see, the whole class is defined within the pair of braces. Its data members are explicitly declared with the has keyword, and there's no need to return 1 at the end of the file.

Now, create an object of the class and increment the internal counter three times, like we did in the Perl 5 example earlier. This is how you do it in Perl 6:

my $var = MyClass.new;

say $var.inc;
say $var.inc;
say $var.inc;

Do not focus on the details yet because it will all be explained in later chapters.

So far, we've seen three examples of where it was desired to improve the syntax of Perl 5.

To see more examples of changes between Perl 5 and Perl 6, you may refer to a few articles grouped under the title 'Perl 5 to Perl 6 guide' in the documentation of Perl 6 at https://docs.perl6.org/language.html, which is dedicated to that specific topic:

5to6-nutshell

Perl 5 to Perl 6, in a nutshell—How do I do what I used to do?

5to6-perlfunc Perl 5 to Perl 6 guide—functions
5to6-perlop Perl 5 to Perl 6 guide—operators
5to6-perlsyn Perl 5 to Perl 6 guide—syntax
5to6-perlvar Perl 5 to Perl 6 guide—special variables

Compatibility with Perl 5

Existing Perl 6 compilers cannot execute Perl 5 programs without modifications in the source code. Perl 5 and Perl 6 are sometimes called sister languages. Both share the same spirit of Perl, and, in many cases, it is possible to convert the program from Perl 5 to Perl 6.

One of the biggest advantages of Perl 5 is the CPAN (Comprehensive Perl Archive Network). It contains a myriad of modules for the immense number of areas. Most probably, your task is already solved by some author of CPAN. To use this useful heritage in your programs in Perl 6, you may want to use the Inline::Perl5 module, which allows using an existing Perl 5 module without modifying the source code.

For example, let's take one of the most popular modules in Perl 5, Text::CSV, and embed it in our program in Perl 6.

use Inline::Perl5;
use Text::CSV:from<Perl5>;

my $csv = Text::CSV.new;

$csv.parse('First name,Last name');
say $csv.fields.join("\t");

$csv.parse('Astrid,Lindgren');
say $csv.fields.join("\t");

With Inline::Perl5 enabled, the :from<Perl5> suffix loads the Text::CSV module from Perl 5 module directory. That module must be installed as a regular Perl 5 module from CPAN.

The rest of the program uses the $csv object, which is an instance of Text::CSV. Notice that you have to follow Perl 6 syntax, so, for instance, instead of creating the object with Text::CSV->new use Text::CSV.new. The same applies to calling the parse method: in Perl 5 it would be $csv->parse(), while in Perl 6 you use dot: $csv.parse(). Working with objects in Perl 6 is described in Chapter 8, Object-Oriented Programming.

Luckily, there is already a module Text::CSV for Perl 6. You can find it on the http://modules.perl6.org page. Using Inline::Perl5 can be very useful for those modules on CPAN, which do not yet have equivalents or replacement, written in Perl 6. For example, the following example taken from the module documentation shows how to connect to the database (of course, you need PostgreSQL to be installed to test the example):

use Inline::Perl5;
use DBI:from<Perl5>;

my $dbh = DBI.connect('dbi:Pg:database=test');
my $products = $dbh.selectall_arrayref(
'select * from products', {Slice => {}}
);

The Inline::Perl5 module is available at https://github.com/niner/Inline-Perl5.

Previous PageNext Page
You have been reading a chapter from
Perl 6 Deep Dive
Published in: Sep 2017Publisher: PacktISBN-13: 9781787282049
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Andrew Shitov

Andrew Shitov has been a Perl enthusiast since the end of the 1990s, and is the organizer of over 30 Perl conferences in eight countries. He worked as a developer and CTO in leading web-development companies, such as Art. Lebedev Studio, Booking dotCom, and eBay, and he learned from the "Fathers of the Russian Internet", Artemy Lebedev and Anton Nossik. Andrew has been following the Perl 6 development since its beginning in 2000. He ran a blog dedicated to the language, published a series of articles in the Pragmatic Perl magazine, and gives talks about Perl 6 at various Perl events. In 2017, he published the Perl 6 at a Glance book by DeepText, which was the first book on Perl 6 published after the first stable release of the language specification.
Read more about Andrew Shitov