Author: lwall
Date: 2009-06-16 19:56:45 +0200 (Tue, 16 Jun 2009)
New Revision: 27091

Modified:
   docs/Perl6/Spec/S04-control.pod
Log:
[S04] require temp semantics for topical statement modifiers


Modified: docs/Perl6/Spec/S04-control.pod
===================================================================
--- docs/Perl6/Spec/S04-control.pod     2009-06-16 16:34:27 UTC (rev 27090)
+++ docs/Perl6/Spec/S04-control.pod     2009-06-16 17:56:45 UTC (rev 27091)
@@ -12,8 +12,8 @@
 
   Maintainer: Larry Wall <la...@wall.org>
   Date: 19 Aug 2004
-  Last Modified: 29 May 2009
-  Version: 79
+  Last Modified: 16 Jun 2009
+  Version: 80
 
 This document summarizes Apocalypse 4, which covers the block and
 statement syntax of Perl.
@@ -495,12 +495,25 @@
 
 so you can modify the current list element in that case.
 
-When used as statement modifiers, C<for> and C<given> use a private
-instance of C<$_> for the left side of the statement.  The outer C<$_>
-can be referred to as C<$OUTER::_>.  (And yes, this implies that the
-compiler may have to retroactively change the binding of C<$_> on the
-left side.  But it's what people expect of a pronoun like "it".)
+When used as statement modifiers on implicit blocks (thunks), C<for>
+and C<given> privately temporize the current value of C<$_> for the
+left side of the statement and restore the original value at loop exit:
 
+    $_ = 42;
+    .say             # 42
+    .say for 1,2,3;  # 1,2,3
+    .say;            # 42
+
+The previous value of C<$_> is not available within the loop.  If you
+want it to be available, you must rewrite it as an explicit block
+using curlies:
+
+    { say OUTER::<$_>, $_ } for 1,2,3;  # 421,422,423
+
+No temporization is necessary with the explicit form since C<$_> is a
+formal parameter to the block.  Likewise, temporization is never needed
+for C<< statement_control:<for> >> because it always calls a closure.
+
 =head2 The do-once loop
 
 In PerlĀ 5, a bare block is deemed to be a do-once loop.  In PerlĀ 6,
@@ -586,7 +599,7 @@
     # Syntax error: Statement-level placeholder block
     { say $^x };
 
-    # Not an syntax error, though $x doesn't get the argument it wants
+    # Not a syntax error, though $x doesn't get the argument it wants
     do { say $^x };
 
     # Not an error: Equivalent to "for 1..10 -> $x { say $x }"

Reply via email to