NAME

Perl6::Overview::Control - Control Structure

DESCRIPTION

Loop Structures

    while EXPR { ... }
    until EXPR { ... }
    
    loop { ... };
    loop ($i = 0; $i < 10; $i++) { ... }

    repeat { ... } while EXPR;
    repeat { ... } until EXPR;
    repeat while EXPR { ... }
    repeat until EXPR { ... }
    

For Loop structures

    for @foo { ... }
    for @foo -> $x { ... }
    for @foo -> $x, $y, $z { ... }
    for @foo.kv -> $index, $value { ... }
    
    for %hash.kv -> $key, $value { ... }
    
    for =<> {...} # was while (<>) { ... } in p5
    

Switch/Case style control elements

    given $x { ... }   # $x is now topic for block
    when EXPR { ... }  # perform $_ ~~ EXPR execute block and break
    default { ... }    # same as when true { ... }    

Closure traits

    BEGIN { ... }      # Body executed at compile-time, as soon as possible
    CHECK { ... }      # Body executed at compile-time, as last as possible
    INIT  { ... }      # Body executed at runtime, as soon as possible
    END   { ... }      # Body executed at runtime, as last as possible
    START { ... }      # Body only executed once (per clone), inline

    sub foo {
      START {
        # Initialize environment, e.g. create some files, etc.
      }

      ...;
    }
    foo();  # Runs initialization block
    foo();  # Does not run initialization block again
    foo();  # ditto