Hello Perl6

Ever since I received "Think Perl 6" book as a gift by Neil Bowers at the London Perl Workshop 2017, I have been planning to start learning Perl6 without any luck. I can think of many reasons why. One of the reasons was my confusion where to start. First I thought, I will start from the basics but then I easily get distracted if I don't have a target. Then I realised why not pick one of my Perl5 distributions and convert it into Perl6. That sounded great idea.

I tweeted about my intention and asked for help. Immediately two people kindly came forward, Scimon Proctor and JJ Merelo. With their guidance and support, I decided to create a basic Calculator distribution. Once I got the approval of the two mentors, I started preparing the ground.

First thing first, I need to have latest Perl6 environment.I had installed Rakudo 2016.10 long time ago. So I decided to install the latest Rakudo 2018.10 as suggested here. Installation was smooth without any issues. However post installation when I checked the installed Perl6 version, it was still saying "2016.10". I was confused why it was not telling "2018.10". I decided to consult IRC channel for Perl6 #perl6.

As soon as I logged in, I saw TimoTimo online and he immediately recognised me and said "Hi Mohammad". I met TimoTimo last year at German Perl Workshop. He is a gem of a person. With his help, I fixed the configuration issue.

So now I have working Perl6 environment. Scimon Proctor, suggested App::MI6 to create the skeleton of new distribution.

$ zef install App::Mi6

Now it is time to create my first Perl6 distribution.

$ mi6 new Calculator

With the above magical command, I have a testable distribution. It is time to get hand dirty and start coding. I found this page very handy to get me started with Class in Perl6. In no time, I have basic Calculator class like below (lib/Calculator.pm6):

use v6.c;

class Calculator:ver<0.0.1> {
has Int $.x is required;
has Int $.y is required;

method add() {
return self.x + self.y;
}

method substract() {
return self.x - self.y;
}

method multiply() {
return self.x * self.y;
}

method divide() {
return self.x / self.y;
}
}

Now time to create unit test (t/01-basic.t):

use v6.c;
use Test;
use Calculator;

my $c = Calculator.new( :x<10>, :y<5> );

is $c.add(), 15, '10+5';
is $c.substract(), 5, '10-5';
is $c.multiply(), 50, '10*5';
is $c.divide(), 2, '10/5';

done-testing;

Thats it, I am done. Time to test the code.

$mi6 test

Test passed without any issues.

Not a bad start for someone who waited for more than a year to start. Thanks for all the encouragements.

It is time to polish the code little bit and add the following bits:

a) Make sure x and y, both must be positive integer
b) Make sure y is always smaller than x
c) Add negative test.

I had absolutely no idea, how to do above. I went back to lovely people at IRC channel and saw JJ Merelo and gang available to help. With their support, I managed to got everything sorted. It now looks like below:

use v6.c;

class Calculator:ver<0.0.1> {
has Int $.x is required where * > 0;
has Int $.y is required where * > 0;

method TWEAK(:$!x!, :$!y!) {
die 'ERROR: x should be > y.' unless $!x > $!y;
};

method add() {
return self.x + self.y;
}

method substract() {
return self.x - self.y;
}

method multiply() {
return self.x * self.y;
}

method divide() {
return self.x / self.y;
}
}

And unit test looks like below:


use v6.c;
use Test;
use Calculator;

dies-ok { Calculator.new( :x<10>, :y<20> ) }, 'No Calculator object (x < y)';
dies-ok { Calculator.new( :x<10>, :y<-20> ) }, 'No Calculator object (y < 0)';
dies-ok { Calculator.new( :x<-10>, :y<20> ) }, 'No Calculator object (x < 0)';

my $c = Calculator.new( :x<10>, :y<5> );

isa-ok $c, 'Calculator', 'Calculator object';
is $c.add(), 15, '10+5';
is $c.substract(), 5, '10-5';
is $c.multiply(), 50, '10*5';
is $c.divide(), 2, '10/5';

done-testing;


The above code hosted on GitHub.

I think, there is not much scope left for me here. I will probably learn more about class in Perl6 and then convert one of my distribution to Perl6.

Watch this space, I will be back soon.

Leave a comment

About Mohammad S Anwar

user-pic CPAN Author & Contributor. Co-editor of PerlWeekly newsletter. Write article for http://perl.com, Father of 3 angels. Indian by birth, British by choice.