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.
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.
- Install rakudobrew from GitHub.
git clone https://github.com/tadzik/rakudobrew ~/.rakudobrew export PATH=~/.rakudobrew/bin:$PATH
- Use rakudobrew to build MoarVM.
rakudobrew build moarThat will download the latest stable MoarVM, build it from source, and run some tests.
- Use rakudobrew to install Panda.
rakudobrew build pandaPanda is a simple Perl 6 package manager which can be used to install modules and their prerequisites.
- 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
- Unlike Perl 5, Perl 6 scalars can be explicitly typed if desired. We use the
$resultis 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.
~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.
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.