A Gentle Introduction to Perl 6 Using Rakudo Star

So, Rakudo Star was recently released and I decided to give it a try. So far, I like what I'm seeing.

The below is meant to serve as a gentle introduction to the Perl 6 language, if it seems to be very "baby-Perl-6" that's because I'm learning the language as I'm writing it. Plus, that's why I called it a gentle introduction! :)

Installation on Windows 7

Download the msi file available from GitHub, double-click and answer the questions with "yes".
The Perl 6 executables will be installed under C:\Rakudo\bin.
Open the Control Panel and go to "System and Security" -> "System". In the left side of the window you should see 4 links, click the bottom one - "Advanced system settings". In the window that opens ("System Properties" - "Advanced" tab), click on the "Environment Variables..." button, which is at the bottom. In the bottom part of the window which opens, there is a "System variables" window. Scroll down to the "Path" variable, make sure it is selected and hit the "Edit..." button. Jump to the end of the "Variable value" field and add ";C:\Rakudo\bin" to the end - the ";" is important. Hit "OK", "OK", "OK". Now open a new cmd.exe window and type "perl6 -v". You should get the output:

This is Rakudo Perl 6, version 2010.07-47-g9fd5eaa built on parrot 2.6.0 Copyright 2008-2010, The Perl Foundation

Congratulations, you're ready to program in Perl 6 on Windows 7 :)

Hello, world!

Open your text editor, type:
say 'Hello, world!';

Save the file as hello.p6. In your cmd.exe window cd to the directory where you saved hello.p6 and type "perl6 hello.p6". You should now see the output:

Hello, world!

Already in this simple program we see a few elements of Perl 6:

  1. "say" is a function which prints to the standard output (in this case) the given argument.
  2. 'Hello, world!' is a string literal. A string is a piece of text, and a string literal is a string which appears directly in the program. There are 2 types of string literals in Perl 6, double-quoted (e.g. "hello") and single quoted (e.g. 'hello'). Since we're not using any variables in this example, there is no need to interpolate, so we use single-quotes.
  3. A Perl 6 program consists of zero or more statements. A statement ends with a semicolon or a curly bracket at the end of a line. In this case, a semicolon.

use v6;

Open hello.p6 in your editor and type as the first line:

use v6;

Your program should now look like:

use v6; say 'Hello, world!';

While not strictly required, every Perl 6 program should begin with "use v6;". This line tells the compiler which version of Perl the program expects. Should you accidentally run the file with Perl 5, you'll get a helpful error message.

Semicolons and Newlines

Now that we have 2 statements we can see that in Perl 6 a statement really does end with a semicolon. Change the program to a single line, like so:

use v6; say 'Hello, world!';

If you run this program it will run the same as before. Perl 6 does not care whether there is a newline or not between statements. Vice-versa, if you forget the semicolon and only use a newline, you'll get an error:

use v6 say 'Hello, world!';


Confused at line 1, near "use v6\r\nsa"

The "\r\n" is the way Windows refers to a newline. I'll usually call it simply "\n" which is the Unix way.

As the last statement in the file, you could if you wanted remove the ';' at the end of the say line. However it is good practise to keep the ';' for the last statement, in case in the future you should modify the file by adding more statements.


Comments start with a # sign and last till the end of the line. A "#" sign inside a string however does not start a comment.
Synopys 02 has a lot more to say about whitespaces and comments.


Apart from simply printing a string's contents there are a variety of other interesting things we can do with strings in Perl 6. For example:

'Hello, world!'.say; # same as "say 'Hello, world!'" 'Hello, world!'.chars.say; # prints "13" which is the length in characters of the string. This also shows that chaining of methods works. 'Hello, world!'.substr(7).say; # prints "world!". Offsets starts at 0. 'Hello, world!'.substr(0,4).say; # prints the first 4 characters 'Hello, world!'.uc.say; # prints "HELLO, WORLD!" 'Hello, world!'.lc.say; # prints "hello, world!" 'Hello, world!'.capitalize.say; # prints "Hello, World!", i.e. it returns a lower case copy of the string with each first character in a word as upper case. 'Hello, world!'.chop.say; # prints "Hello, world". You get extra points for using ".chop.chop" in a program :) 'Hello, world!'.flip.say; # prints "!dlrow ,olleH"

You can see these methods and more documented at the S32 - Str Synopsys page.

Strings in Perl 6 can be concatenated using the tilda (~) operator.

say 'Hello,' ~ ' world!'; # prints "Hello, world!"

If you're dealing with Unicode strings, make sure read the Perl 6 S32 document mentioned above, since you will need to be aware of certain finer points of how Unicode text is handeled in Perl 6.


Variables in Perl 6 have names which start with a sigil, which is non-alpha-numeric symbol such as $, @, %, or & - or occasionally the double colon ::. The sigils usually restrict the variable to a particular type, such as a single value or a compound value. After the sigil comes an identifier, which may consist of letters, digits and the underscore. Between letters you can also use a dash - or an apostrophe ', so isn't and double-click are valid identifiers. First of all I'd like to talk about scalars.

my $x; $x = 42; # Decimal Integer $x = 0xF6; # Hexadecimal Integer $x = 0b1010010001; # Binary Integer $x = 3.1415; # Floating Point Number $x = 2.34E-5; # Scientific Notation $x = "Hello "; # Double-Quoted String $x = 'World!'; # Single-Quoted String $x = MyObject.new(); # Object Reference

The "my" is another Perl 6 function, this one declares a variable local to the enclosing scope (or till the end of the file, if decalred in the file scope). For those coming from a Perl 5 background, using a variable before declaring it (with "my" for example) will no longer work. You must, in other words, declare a variable, for example with "my", before trying to use it.

Note that declaring "$x = 42" does not prevent Perl 6 from treating 42 as a string, and vice-versa:

my $x = 42; "The answer to life, the universe and everything is... $x!".say ;# works as expected my $sum = 1 + "5"; $sum.say; ;# prints 6, the string "5" is used as a number.

See S02 for a few more advanced ways to define a scalar variable, e.g. "my Int $x;". Not going to talk about that here, this is a gentle introduction :)

More to come at later time: arrays, hashes, flow control and of course, regular expressions. That's all for now :)

Leave a comment

About Offer Kaye

user-pic I blog about Perl.