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.
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:
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:

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:

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".

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

That's It!
You now know how to use the Pugs interactive environment and how to write and run Perl 6 programs! What next?- Check out the Programmer's Heaven Perl 6 FAQ for answers to over one hundred Perl 6 questions.
- Subscribe to our Perl 6 Newsletter, sent out every couple of weeks.
- Discuss Perl 6 on our Perl 6 forum.
Sponsored links
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
CSTSOFT Instrumentation .NET & ActiveX Components
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.
Software Localization Tool Sisulizer
Localize DotNet, C++ Builder, Delphi, C/C++, Visual Basic & Java apps & html help. Try Sisulizer now
Localize DotNet, C++ Builder, Delphi, C/C++, Visual Basic & Java apps & html help. Try Sisulizer now
Attend WINDOWS EMBEDDED ACCELERATION WORKSHOPS
Are you ready to learn how you can bring your next-generation embedded device to market faster?
Are you ready to learn how you can bring your next-generation embedded device to market faster?
SFTP components for .NET
The components for the most complete SFTP and SSH support in your .NET, Mono or .NET CF application
The components for the most complete SFTP and SSH support in your .NET, Mono or .NET CF application