Documentation for syntax
blocks assembled from the following types:
From Control Flow
Like many languages, Perl 6 uses
blocks enclosed by
} 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
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␤»; # 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"; ; 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# OUTPUT: «42␤»# OUTPUT: «43␤»; # OUTPUT: «42␤43␤»
# Syntax error# Also a syntax error, of course
So, be careful when you backspace in a line-wrapping editor:
\# 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.
From Perl 6 Pod
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
#|, 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.
WHY method can be used on these classes, roles, subroutines etc. to return the attached Pod value.
sub duel(Magician , Magician )say Magician.WHY; # OUTPUT: «Base class for magicians␤»say .WHY.leading; # OUTPUT: «Fight mechanics␤»say .WHY.trailing; # OUTPUT: «Magicians only, no mortals.␤»