Perl 6 Quick Start

So you want to write your first Perl 6 program but don't know your Pugs from your pointy block? Help is at hand!

What do I need?

A little time aside, there are only three things that you need to write and run your first Perl 6 program:
  • Something to run Perl 6 programs with. We will use Pugs, since it is the most complete Perl 6 implementation at the time of writing.
  • Perl 5. Pugs embeds Perl 5 in order to support using Perl 5 modules in Perl 6 programs.
  • A text editor to write Perl 6 programs in; just use your favourite one.
So, let's go!

Step 0: Get Perl 5

The vast majority of Linux and UNIX based systems will have Perl 5 installed by default. Windows users can download ActivePerl for free; ActivePerl works on a number of other platforms too. If your platform is not supported by ActivePerl, you may find this page helpful.

Step 1: Get Pugs

If you are a Windows user, you can download a binary build of Pugs. Click here to go to the download page. Once you have downloaded it, open up the zip file and copy the files somewhere convenient, such as "C:\Pugs". Then go to the next step.

If you do not use Windows, you will need to compile Pugs yourself. To do this you will need standard build tools for compiling software and the Glasgow Haskell compiler, which you can get here (OK, OK, so I lied about only needing two things if you aren't a Windows user). Then get the Pugs source, either from CPAN or by checking it out using Subversion:

svn co http://svn.openfoundry.org/pugs


You can then build Pugs like this:

cd pugs
perl Makefile.PL
make


Step 2: Run Pugs

If you are on Windows, double click "pugs.exe", which you extracted from the ZIP file you downloaded. If you have built your own Pugs, just type "pugs" at the command line and press enter. You will then be presented with something like this:

Pugs Startup Screen


Step 3: Use The Pugs Interactive Interface

The "pugs>" command line allows you to write and run Perl 6 code right away. I guess we should start with the obligatory "Hello, world". Enter:

say "Hello, world!";


And press return. You will then see something like this:

Pugs running Hello World program


There are two lines of output. The first is, as expected, the string "Hello, world!". The second, "Bool::True", gives the return value of the "say" function. You can write expressions directly into Pugs too:

pugs> 25 + 17
42


We can declare variables too, for example an array:

my @pets = ('dog', 'cat', 'badger', 'monkey');


Let's try iterating over the array that we just declared and printing each of the elements.

for @pets -> $pet {
    say "I have a $pet";
}


If you open a block, then Pugs will not evaluate it until you enter the closing brace:

Entering a loop in Pugs


Once you enter the closing brace and press enter, the program is executed and the following output is produced:

I have a dog
I have a cat
I have a badger
I have a monkey


To leave the Pugs interactive environment, type ":q" and press enter.

Step 4: Write a Perl 6 Program

Now let's write a program in a text editor and run this with Pugs. Open up your preferred editor and enter the following program:

# Get someone to enter their age.
print "How old are you? ";
my $age = $*IN.readline;
# Give 'em a message depending on their age.
if $age < 0 {
    say "You can't type, you ain't been born yet!";
} elsif 0 <= $age <= 12 {
    say "You're only a kid.";
} elsif 13 <= $age <= 19 {
    say "ERGH! You're a teenager.";
} elsif 20 <= $age <= 65 {
    say "You're an adult.";
} else {
    say "My gosh, you're old!";
}


And save it "age.p6".

Writing a Perl 6 program, in Notepad of all things...


You can then run it from the command line, using Pugs.

Running a Perl 6 program


That's It!

You now know how to use the Pugs interactive environment and how to write and run Perl 6 programs! What next?

Happy hacking!
 

Other Views

corner

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.