Programmers Heaven jobs
.NET Developer
Device Driver Developer (Lead Engineer)
Software Programmer/VR Programmer/Software Enginee


More jobs                Post a job


Perl 6 FAQ - Basic Constructs

This FAQ is part of the Programmer's Heaven Perl 6 FAQ. It covers some of the basic syntax of the Perl 6 language.

How do I print some text?

Perl 6 has retained the "print" keyword from Perl 5 and added a new keyword, "say". Unlike "print", the "say" keyword automatically adds a line break at the end of the text. Therefore, the following two lines of code are equivalent:

print "hello, mum\n";
say "hello, mum";


Note that "\n" is the new line character.

How do I write an if block?

Perl 6 mostly retains the same syntax that Perl 5 had for "if" statements, and will also look familiar to programmers from C-like languages. The difference is that in Perl 6 you no longer need to put brackets around the condition.

if condition {
          1. condition true
} elsif condition 2 {
          1. condition 2 true
} else {
          1. other conditions false
}


Here is a real example in Perl 6.

my $x = 5;
if $x == 5 {
    say "x is 5!";      # Prints "x is 5!"
} else {
    say "x is not 5!";  # Does not print
}


In Perl 5 this would be written as:

my $x = 5;
if ($x == 5) {
    print "x is 5!\n";      # Prints "x is 5!"
} else {
    print "x is not 5!\n";  # Does not print
}


How do I write a for style loop?

Perl 5 and many C-like languages support a loop construct involving an initial condition, a termination condition and what to do at the end of each iteration. This is also available in Perl 6, but now you must use the loop keyword.

This program prints the numbers from 1 to 10, each on a line of its own.

loop ($i = 1; $i <= 10; $i++) {
    say $i;
}


Note that, unlike other Perl 6 constructs, you need the brackets here. You will probably find yourself writing loops like this a lot less often than you do in other languages, due to easy iteration over elements of arrays and other Perl 6 language features.

In Perl 5 the "for" keyword did both this style of iteration and iteration over arrays; Perl 6 tidies up and gives this case a keyword of its own. Note that you can write an infinite loop easily now too:

loop {
    ...
}


How do I iterate (loop) over the elements of an array?

Perl 6 has the "for" keyword to do this (as in Perl 5), but the syntax is different. We now use "->" to feed each value from the array into a variable. For example:

my @friends = ('David', 'Jonny', 'Gary');
for @friends -> $name {
    say "$name is my friend."
}


Perl 6 also offers you some extra flexibility here; it is possible to take multiple entries from the array per iteration. The following program:

my @a = ("pie","chips","bangers","mash");
for @a -> $a, $b {
    say "$a and $b";
}


Will produce the output:

pie and chips
bangers and mash


The "foreach" keyword from Perl 5 has been removed in Perl 6; always use "for". If you miss out the "-> $name" part you just get the value in the default variable, $_.

This answer somewhat glosses over what "->" really is; to fully understand it, read up on pointy blocks in the Subroutines FAQ.

How do I write a while loop?

Perl 6 retains the while keyword from Perl 5 and many other languages. However, brackets are not needed around the condition.

my $x = 1;
while $x <= 10 {
    say $x;
    $x++;
}



Back To FAQ List | Next Section



What's next?


Join our Perl 6 Newsletter
Email:



Visit our Perl Resources


  User Comments


riya

From India
(Report as abusive)
"Very Useful"
this faqs made to know about many things unknown and it helped me a lot
  View all   Rate and comment this article




 
Printer friendly version of the Perl6-FAQ-Basics page


Sponsored links

PureCM Software Configuration Management
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.
Software Localization Tool Sisulizer
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?
SSH and SFTP support for .NET
Add complete SSH and SFTP support to your .NET framework application


Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd 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 Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.