Skip to content

Commit 7187fee

Browse files
committed
Decided example of when statement modifier disrupted the flow
Explain it when explaining proceed/succeed instead Also, document nested when/default statements
1 parent 703ee75 commit 7187fee

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

doc/Language/control.pod

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,6 @@ as though the rest of the statements in the block are skipped.
397397
A C<when> statement will also do this (but a C<when> statement modifier
398398
will I<not>.)
399399
400-
given 42 {
401-
"This says".say when 42; # just a prettier 'if $_ ~~ Int'
402-
when Int { "This also says".say };
403-
"This never says".say;
404-
}
405-
406400
In addition, C<when> statements C<smartmatch> the topic (C<$_>) against
407401
a supplied expression such that it is possible to check against values,
408402
regular expressions, and types when specifying a match.
@@ -424,6 +418,20 @@ C<when> statements. The following code says C<"Int"> not C<42>.
424418
}
425419
#-> Int
426420
421+
When a C<when> statement or C<default> statement causes the outer
422+
block to return, nesting C<when> or C<default> blocks do not count
423+
as the outer block, so you can nest these statements and still
424+
be in the same "switch" just so long as you do not open a new block:
425+
426+
given 42 {
427+
when Int {
428+
when 42 { say 42 }
429+
say "Int"
430+
}
431+
default { say "huh?" }
432+
}
433+
#-> 42
434+
427435
=head3 X<proceed and succeed|control flow,proceed succeed>
428436
429437
Both C<proceed> and C<succeed> are meant to be used only from inside C<when>
@@ -486,6 +494,22 @@ specify a final value for the block.
486494
}
487495
#-> Int
488496
497+
If you are not inside a when or default block, it is an error to try
498+
to use C<proceed> or C<succeed>. Also remember, the C<when> statement
499+
modifier form does not cause any blocks to be left, and any C<succeed>
500+
or C<proceed> in such a statement applies to the surrounding clause,
501+
if there is one:
502+
503+
given 42 {
504+
{ say "This says" } when Int;
505+
"This says too".say;
506+
when * > 41 {
507+
{ "And this says".say; proceed } when * > 41;
508+
"This never says".say;
509+
}
510+
"This also says".say;
511+
}
512+
489513
=head2 X<loop|control flow>
490514
491515
The C<loop> statement is the C-style C<for> loop in disguise:

0 commit comments

Comments
 (0)