Exploring Perl 6: Up and Running

| 4 Comments | No TrackBacks

Happy new year!

After a remarkably long wait, several false starts, and an unfathomable amount of work by the community, Perl 6 is here. Let's see just how easy it is to get up and running and to write a few simple programs.

Jargon

Before we get started, let's clarify some terminology.

  • Perl 6 is a programming language specification. It's not the next version of Perl 5 (which remains under active development) but a whole new language.
  • Rakudo is a compiler implementing the Perl 6 specification. There are other Perl 6 compilers, but Rakudo is the only one that is complete today.
  • MoarVM is one of the virtual machine architectures targeted by Rakudo. Rakudo also targets the JVM, but the MoarVM backend is considered more stable at this time.
  • Rakudo Star is a distribution of the Rakudo compiler plus a bunch of Perl 6 modules and documentation to make it useful. These blog posts will focus on the use of Rakudo Star.

Installing Rakudo Star

Currently, the easiest way to build Rakudo Perl 6 from source is to use the rakudobrew script available on GitHub. (You can also find pre-built Rakudo Star builds for various platform package managers, but rakudobrew will get you the latest version of everything.)

Here's how I did my install on MacOS X Yosemite.

  1. Install rakudobrew from GitHub.
    git clone https://github.com/tadzik/rakudobrew ~/.rakudobrew
    export PATH=~/.rakudobrew/bin:$PATH
             
  2. Use rakudobrew to build MoarVM.
    rakudobrew build moar
             
    That will download the latest stable MoarVM, build it from source, and run some tests.
  3. Use rakudobrew to install Panda.
    rakudobrew build panda
            
    Panda is a simple Perl 6 package manager which can be used to install modules and their prerequisites.
  4. Use Panda to install the Rakudo Star modules and documentation.
    panda install Task::Star
            

If everything worked, we should now have a fully-functional Perl 6 installation with several useful modules to play around with. Let's write a program!

#!/usr/bin/env perl6
# file ex1.pl6

say "Hello, world! I am Perl 6!";

my Int $foo = 1;
my Int $bar = 3;
my $result = $foo / $bar;

say "$foo / $bar is $result.";
say "$result times 3 is " ~ $result * 3;

Running this code should produce the following result:

perl6 ex1.pl6
Hello, world! I am Perl 6!
1 / 3 is 0.333333.
0.333333 times 3 is 1

What does it do?

  • Like Perl 5, Perl 6 has scalars that can hold a single number, string, object, etc. Scalars are indicated by the $ sigil.
  • Unlike Perl 5, Perl 6 scalars can be explicitly typed if desired. We use the Int type for $foo and $bar.
  • Since $result is not explicitly typed, it can contain anything. In this case, a division of two Ints will get us a Rat, Perl 6's builtin type for rational numbers.
  • The ~ operator concatenates strings.
  • Thanks to Perl 6's native support for rationals, we actually got the right answer to a simple arithmetic program that trips up languages which can only handle floats natively.

Technology...strike one...

Perl 6 has a wealth of compelling features that make it an exciting addition to the dynamic language community. In upcoming posts we'll dive a lot deeper into what makes Perl 6 a remarkable new technology.

No TrackBacks

TrackBack URL: http://friedo.com/cgi-bin/mt/mt-tb.cgi/27

4 Comments

panda and perl6 aren't in my path after building them. they appear to live in moar-nom/panda/bin/ and moar-nom/, respectively.

Mike, this looks like a very promising and useful guide to getting people up and running with Perl6. I'm considering getting together a P6 study group in New York City and I will include this as a recommended reading.

Thank you very much.
Jim Keenan




The installation examples worked fine on my Ubuntu Linux x86_64 machine.



$ rakudobrew build moar
...
/home/jkeenan/perl5/perlbrew/perls/perl-5.22.0/bin/perl -MExtUtils::Command -e chmod 755 /home/jkeenan/.rakudobrew/moar-nom/install/bin/perl6


Rakudo has been built and installed.
Updating shims
Switching to moar-nom
Done, moar-nom built
$ man rakudo
No manual entry for rakudo
$ perl6 -e 'print "Hello world\n"'
Hello world
$ which perl6
/home/jkeenan/.rakudobrew/bin/perl6


One suggestion: Typically I do not write sample programs in my home directory. I created a directory underneath my home directory in which to transcribe the program in this post.



$ mkdir -p ~/learn/p6


But in order to run the program I had to remember to add the directory for Rakudo to my path and activate it.



$ echo 'export PATH=~/.rakudobrew/bin:$PATH' >> ~/.bashrc
$ tail -1 ~/.bashrc
export PATH=~/.rakudobrew/bin:$PATH
$ cd ~/p6
$ perl6 ex1.p6
Hello, world! I am Perl 6!
1 / 3 is 0.333333.
0.333333 times 3 is 1



Leave a comment

About this Entry

This page contains a single entry by Mike Friedman published on January 3, 2016 11:01 PM.

(Ab)using the MongoDB Aggregation Framework for Symbolic Computation was the previous entry in this blog.

Exploring Perl 6: Sigil Invariance is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

  • Exploring Perl 6

Pages

Powered by Movable Type 5.14-en