Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Smooth out and simplify the section on blocks a bit
  • Loading branch information
skids committed Dec 13, 2015
1 parent 48d263a commit 621eb8a
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions doc/Language/control.pod
Expand Up @@ -23,24 +23,38 @@ also be written as:
=head2 X<blocks|control flow>
Like many languages, Perl6 uses C<blocks> delimited by C<{> and C<}>
to compartmentalize code. When a block stands alone as a statement,
it will be entered immediately after the statement before it finishes,
and the statements inside it will be executed. Otherwise, a block
simply creates a closure, which may be executed at a later time:
Like many languages, Perl6 uses C<blocks> enclosed by C<{> and C<}> 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 C<}>.
{ 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; # says "1"
{ say 2; say 3 }; # says "2" then says "3"
say 4; # says "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
L<elsewhere|language/functions#Blocks and Lambdas>. 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 it instantiates
an object of type C<Block>, but does not run it. Since any object instance
is true, the code does not die, even though that block would evaluate to 0,
were it to be executed.
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 C<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 C<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.
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
Expand Down

0 comments on commit 621eb8a

Please sign in to comment.