Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Split up some long lines and go OCD on the code sample spacing
Polish a phrase here or there
  • Loading branch information
skids committed Apr 10, 2015
1 parent 561d1c6 commit 49755ef
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions lib/Language/control.pod
Expand Up @@ -20,8 +20,8 @@
The C<given> statement is Perl 6's topicalizing keyword in the same way that
C<switch> topicalizes in languages such as C. The keyword for individual
cases is C<when> and C<default> for the default case. In general this form
of control flow statement is written thus:
cases is C<when> and C<default> for the default case. The usual idiom
looks like this:
given EXPR {
when EXPR { ... }
Expand All @@ -31,48 +31,51 @@ of control flow statement is written thus:
The C<when> expressions C<EXPR> are smart matched against the given value
such that it is possible to check against values, regular expressions, and
types when specifying a match
types when specifying a match.
given 42 {
when Int { .say }
default { say "how did I get here?" }
default { say "how did I get here?" }
}
#-> 42
In this form, the C<given>/C<when> construct acts much like a set of
C<if>/C<elsif>/C<else> statements. Thus it is necessary to be careful with the order of the C<when> statements. Hence the following code won't match with the value C<42>:
C<if>/C<elsif>/C<else> statements. Thus it is necessary to be careful
with the order of the C<when> statements. The following code says
C<"Int"> not C<42>.
given 42 {
when Int { say "Int" }
when 42 { say 42 }
default { say "huh?" }
when 42 { say 42 }
default { say "huh?" }
}
#-> Int
To get around this issue, one can use the C<proceed> and C<succeed>
statements. C<proceed> will match once more after a successful match, like so:
To enter multiple when blocks one can use the C<proceed> and C<succeed>
statements. C<proceed> will match once more after a successful
match, like so:
given 42 {
when Int { say "Int"; proceed }
when 42 { say 42 }
when Int { say "Int"; proceed }
when 42 { say 42 }
when 40..* { say "greater than 40" }
default { say "huh?" }
default { say "huh?" }
}
# -> Int
# -> 42
Note that the C<when 40..*> match didn't occur. For this to match such cases as
well, one would need a proceed in the C<when 42> block.
Note that the C<when 40..*> match didn't occur. For this to match
such cases as well, one would need a proceed in the C<when 42> block.
To make it clearer that C<proceed> doesn't merely attempt to match the
directly following block, however will attempt to match the C<given> value
once more, consider this code:
This is not like a C<C> C<switch> statement, because the C<proceed> does
not merely enter the directly following block, it attempts to match
the C<given> value once more, consider this code:
given 42 {
when Int { "Int".say; proceed }
when 43 { 43.say }
when 42 { 42.say }
default { "got change for an existential answer?".say }
when 43 { 43.say }
when 42 { 42.say }
default { "got change for an existential answer?".say }
}
#-> Int
#-> 42
Expand Down

0 comments on commit 49755ef

Please sign in to comment.