Sun, 21 Sep 2008

Basic Control Structures


Permanent link

NAME

"Perl 5 to 6" Lesson 03 - Basic Control Structures

SYNOPSIS

    if $percent > 100  {
        say "weird mathematics";
    }
    for 1..3 {
        # using $_ as loop variable
        say 2 * $_;
    }
    for 1..3 -> $x {
        # with explicit loop variable
        say 2 * $x;
    }

    while $stuff.is_wrong {
        $stuff.try_to_make_right;
    }

    die "Access denied" unless $password eq "Secret";

DESCRIPTION

Most Perl 5 control structures are quite similar in Perl 6. The biggest visual difference is that you don't need a pair of parenthesis after if, while, for etc.

Branches

if is mostly unchanged, you can still add elsif and else branches. unless is still there, but no else branch is allowed after unless.

    if $sheep == 0 {
        say "How boring";
    } elsif $sheep == 1 {
        say "One lonely sheep";
    } else {
        say "A herd, how lovely!";
    }

You can also use if and unless as a statement modifier, i.e. after a statements:

    say "you won" if $answer == 42;

Loops

You can manipulate loops with next and last just like in Perl 5.

The for-Loop is now only used to iterate over lists. By default the standard variable $_ is used, unless an explicit loop variable is given.

    for 1..10 -> $x {
        say $x;
    }

The -> $x { ... } thing is called a "pointy block" and is something like an anonymous sub, or a lambda in lisp.

You can also use more than one loop variable:

    for 0..5 -> $even, $odd {
        say "Even: $even \t Odd: $odd";
    }

This is also how you iterate over hashes:

    my %h = a => 1, b => 2, c => 3;
    for %h.kv -> $key, $value {
        say "$key: $value";
    }

The C-style for-loop is now called loop:

    loop (my $x = 1; $x < 100; $x = $x**2) {
        say $x;
    }

SEE ALSO

http://perlcabal.org/syn/S04.html#Conditional_statements

[/perl-5-to-6] Permanent link

Comments / Trackbacks:

Trackback URL: /blog-en/perl-5-to-6/03-control-structures.trackback

Shawn Boyette wrote


Thank you! This is an excellent series, and it's clearly written. I'm looking forward to future posts :)

Kris wrote


Thanks... I have to agree that these posts should be very useful. Just a quick note: I subscribed to your RSS feed, and it seems that there are no absolute URLs in the links. This makes it difficult to get from my RSS reader back to the original article. Here are details from the Feed Validator: http://feedvalidator.org/check.cgi?url=perlgeek.de%2Fblog-en%2Findex.rss

chudpi wrote

Fantastic!
Invaluable for the common coder. Excellently written. Impatiently watching the RSS feed for more! Thank you! P.S. Are you getting a grant from the Perl Foundation for this? I think the value this series will provide by "bringing Perl 6 to the masses" certainly warrants one.

Tav wrote


so multis are basicaly overloading?

Moritz wrote

Thanks for all the feedback
The RSS Feed should now contain absolute links, thanks for pointint that out. Yes, multis are what C++ programmers know as overloading. No, I haven't applied for a TPF grant, I do it for fun (and for promoting my blog, with some success ;-)) and hope that some more money is us for funding real coding work.

bennymack wrote

infinite loop
I believe your loop example generates an infinite loop.

Write a comment

The comments on this blog post have been disabled; the comment form below will not work.

 
Name:
URL: [http://www.example.com/] (optional)
Title: (optional)
Comments:
Save my Name and URL/Email for next time