syntax blocks

Documentation for syntax blocks assembled from the following types:

language documentation Control Flow

From Control Flow

(Control Flow) control flow blocks

Like many languages, Perl 6 uses blocks enclosed by { and } to turn multiple statements into a single statement. It is ok to skip the semicolon between the last statement in a block and the closing }.

{ say "Hello"say "World" }

When a block stands alone as a statement, it will be entered immediately after the previous statement finishes, and the statements inside it will be executed.

say 1;                    # OUTPUT: «1␤» 
{ say 2say 3 };         # OUTPUT: «2␤3␤» 
say 4;                    # OUTPUT: «4␤» 

Unless it stands alone as a statement, a block simply creates a closure. The statements inside are not executed immediately. Closures are another topic and how they are used is explained elsewhere. For now it is just important to understand when blocks run and when they do not:

say "We get here"{ say "then here." }{ say "not here"0} or die;

In the above example, after running the first statement, the first block stands alone as a second statement, so we run the statement inside it. The second block does not stand alone as a statement, so instead, it makes an object of type Block but does not run it. Object instances are usually considered to be true, so the code does not die, even though that block would evaluate to 0, were it to be executed. The example does not say what to do with the Block object, so it just gets thrown away.

Most of the flow control constructs covered below are just ways to tell perl6 when, how, and how many times, to enter blocks like that second block.

Before we go into those, an important side-note on syntax: If there is nothing (or nothing but comments) on a line after a closing curly brace where you would normally put semicolon, then you do not need the semicolon:

# All three of these lines can appear as a group, as is, in a program 
{ 42.say }                # OUTPUT: «42␤» 
{ 43.say }                # OUTPUT: «43␤» 
{ 42.say }{ 43.say }    # OUTPUT: «42␤43␤» 


{ 42.say }  { 43.say }    # Syntax error 
{ 42.say} { 43.say }    # Also a syntax error, of course 

So, be careful when you backspace in a line-wrapping editor:

{ "Without semicolons line-wrapping can be a bit treacherous.".say } \
{ 43.say } # Syntax error 

You have to watch out for this in most languages anyway to prevent things from getting accidentally commented out. Many of the examples below may have unnecessary semicolons for clarity.

language documentation Perl 6 Pod

From Perl 6 Pod

(Perl 6 Pod) declarator blocks

Declarator blocks differ from the others by not having a specific type, instead they are attached to some source code.

Declarator blocks are introduced by a special comment: either #= or #|, which must be immediately followed by either a space or an opening bracket. If followed by a space, the block is terminated by the end of line; if followed by one or more opening brackets, the block is terminated by the matching sequence of closing brackets.

Blocks starting with #| are attached to the code after them, and blocks starting with #= are attached to the code before them.

Since declarator blocks are attached to source code, they can be used to document classes, roles, subroutines etc.

The WHY method can be used on these classes, roles, subroutines etc. to return the attached Pod value.

#| Base class for magicians 
class Magician {
  has Int $.level;
  has Str @.spells;
#| Fight mechanics 
sub duel(Magician $aMagician $b{
#= Magicians only, no mortals. 
say Magician.WHY# OUTPUT: «Base class for magicians␤» 
say &duel.WHY.leading# OUTPUT: «Fight mechanics␤» 
say &duel.WHY.trailing# OUTPUT: «Magicians only, no mortals.␤»