diff --git a/S04-control.pod b/S04-control.pod index 6d8cb26df..de986ef08 100644 --- a/S04-control.pod +++ b/S04-control.pod @@ -908,21 +908,27 @@ English topicalizer, C. The keyword for individual cases is C: } The current topic is always aliased to the special variable C<$_>. The -C block is just one way to set the current topic, but a switch -statement can be any block that sets C<$_>, including a C loop -(assuming one of its loop variables is bound to C<$_>) or the body of a -method (if you have declared the invocant as C<$_>). So switching behavior -is actually caused by the C statements in the block, not by the nature -of the block itself. A C statement implicitly does a "smart match" -between the current topic (C<$_>) and the argument of the C. If the -smart match succeeds, C's associated block is executed, and the -innermost surrounding block that has C<$_> as one of its formal parameters -(either explicit or implicit) is automatically broken out of. (If that is -not the block you wish to leave, you must use the C method (or -some other control exception such as C or C) to be more -specific, since the compiler may find it difficult to guess which -surrounding construct was intended as the actual topicalizer.) The value of -the inner block is returned as the value of the outer block. +C block is just one way to set the current topic. A C loop is +another convenient form (assuming one of its loop variables is bound to +C<$_>). However, since every block that doesn't explicitly take a <$_> +parameter or declare C<$_> will get an implicit C<$_>, you can set that +and use the C and C keywords in it: + + sub seek-the-answer() { + $_ = (^100).pick; + when 42 { say "The answer!" } + default { say "A number" } + } + +So switching behavior is actually caused by the C statements in the +block, not by the nature of the block itself. A C statement implicitly +does a "smart match" between the current topic (C<$_>) and the argument of +the C. If the smart match succeeds, C's associated block is +executed, and the innermost surrounding block is automatically broken out of. +(If that is not the block you wish to leave, you must use the C +method (or some other control exception such as C or C) to be +more specific.) The value of the inner block is returned as the value of +the outer block. If the smart match fails, control proceeds to the next statement normally, which may or may not be a C statement. Since C statements are @@ -950,21 +956,22 @@ implicitly), that parameter can function as the topic of any C statements within the loop. You can explicitly break out of a C block (and its surrounding -topicalizer block) early using the C verb. More precisely, it -first scans outward (lexically) for the innermost containing C block. -From there it continues to scan outward to find the innermost block outside -the C that defines C<$_>, either explicitly or implicitly. (Note that -both of these scans are done at compile time; if the scans fail, it's a -compile-time semantic error.) Typically, such an outer block will be the -block of a C or a C statement, but any block that sets the topic -can be broken out of. At run time, C uses a control exception to -scan up the dynamic chain to find the call frame belonging to that same -outer block, and when it has found that frame, it does a C<.leave> on it to -unwind the call frames. If any arguments are supplied to the C -function, they are passed out via the C method. Since leaving a -block is considered a successful return, breaking out of one with C -is also considered a successful return for the purposes of C and -C. +block) early using the C verb. More precisely, it first scans +outward (lexically) for the innermost containing C block. If that +C block is itself directly inside of a C block, the scan +also skips over that, so you can do nesting such as: + + when * > 2 { + when 4 { 'four!' } + default { 'huge' } + } + default { + 'little' + } + +The surrounding frame is then left, returning the value provided to C. +Breaking out of a block with C is also considered a successful return +for the purposes of C and C. The implicit break of a normal C block works the same way, returning the value of the entire block (normally from its last statement) via an