Author: lwall
Date: 2009-10-10 19:16:31 +0200 (Sat, 10 Oct 2009)
New Revision: 28727

Modified:
   docs/Perl6/Spec/S04-control.pod
   docs/Perl6/Spec/S06-routines.pod
Log:
[S04,S06]
Placeholders may never be used in a hash composer.
Explain how this means map always works right even if it's map { $_ => 1 }
Placeholders @_ and %_ may be used standalone, not just with other placeholders.
Explain how this makes P5 subs fall out naturally, more or less
Clarify that $_ is a placeholder only in the absence of other placeholders.
Mention how if statements don't pass anything to implicit $_ blocks even if
they have an optional $_ in the signature.


Modified: docs/Perl6/Spec/S04-control.pod
===================================================================
--- docs/Perl6/Spec/S04-control.pod     2009-10-10 17:09:55 UTC (rev 28726)
+++ docs/Perl6/Spec/S04-control.pod     2009-10-10 17:16:31 UTC (rev 28727)
@@ -13,8 +13,8 @@
 
     Created: 19 Aug 2004
 
-    Last Modified: 1 Oct 2009
-    Version: 83
+    Last Modified: 10 Oct 2009
+    Version: 84
 
 This document summarizes Apocalypse 4, which covers the block and
 statement syntax of Perl.
@@ -1314,17 +1314,26 @@
 because it will be considered to be two terms in row.
 (Remove the whitespace if you wish it to be a postcircumfix.)
 
-Anywhere a term is expected, a block is taken to be a closure definition
-(an anonymous subroutine).  If the closure is empty, or appears to contain
+Anywhere a term is expected, a block is taken to be a closure
+definition (an anonymous subroutine).  If a closure has arguments,
+it is always taken as a normal closure.  (In addition to standard
+formal parameters, placeholder arguments also count, as do the
+underscore variables.  Implicit use of C<$_> with C<.method> also
+counts as an argument.)
+
+However, if an argumentless closure is empty, or appears to contain
 nothing but a comma-separated list starting with a pair or a hash (counting
 a single pair or hash as a list of one element), the closure will be
-immediately executed as a hash composer.
+immediately executed as a hash composer, as if called with C<.()>.
 
     $hash = { };
     $hash = { %stuff };
     $hash = { "a" => 1 };
     $hash = { "a" => 1, $b, $c, %stuff, @nonsense };
 
+    $code = { %_ };                            # use of %_
+    $code = { "a" => $_ };                     # use of $_
+    $code = { "a" => 1, $b, $c, %stuff, @_ };  # use of @_
     $code = { ; };
     $code = { @stuff };
     $code = { "a", 1 };
@@ -1332,12 +1341,17 @@
 
 If you wish to be less ambiguous, the C<hash> list operator will
 explicitly evaluate a list and compose a hash of the returned value,
-while C<sub> introduces an anonymous subroutine:
+while C<sub> or C<< -> >> introduces an anonymous subroutine:
 
+    $code = -> { "a" => 1 };
     $code = sub { "a" => 1 };
     $hash = hash("a" => 1);
     $hash = hash("a", 1);
 
+Note that the closure in a C<map> will never be interpreted as a hash,
+since such a closure always takes arguments, and use of placeholders
+(including underscore variables) is taken as evidence of arguments.
+
 If a closure is the right argument of the dot operator, the closure
 is interpreted as a hash subscript.
 

Modified: docs/Perl6/Spec/S06-routines.pod
===================================================================
--- docs/Perl6/Spec/S06-routines.pod    2009-10-10 17:09:55 UTC (rev 28726)
+++ docs/Perl6/Spec/S06-routines.pod    2009-10-10 17:16:31 UTC (rev 28727)
@@ -16,8 +16,8 @@
 
     Created: 21 Mar 2003
 
-    Last Modified: 9 Oct 2009
-    Version: 119
+    Last Modified: 10 Oct 2009
+    Version: 120
 
 This document summarizes Apocalypse 6, which covers subroutines and the
 new type system.
@@ -1575,10 +1575,19 @@
 not because we're mean, but because it helps us catch references to
 obsolete PerlĀ 5 variables such as C<$^O>.
 
-A function containing placeholders may also refer to either C<@_>
-or C<%_> or both, each of which (if so used) will be added to the
-signature as a normal readonly slurpy pararmeter:
+The C<$_> variable functions as a placeholder in a block without any
+other placeholders or signature.  Any bare block without placeholders
+really has a parameter like this:
 
+    -> $_ is rw = OUTER::<$_> { .mumble }
+
+(However, statement control C<if> notices this and passes no argument,
+so C<$_> ends up being bound to the outer C<$_> anyway.)
+
+A block may also refer to either C<@_> or C<%_> or both, each of
+which will be added to generated signature as a normal readonly
+slurpy pararmeter:
+
     { say $:what; warn "bad option: $_\n" for keys %_; }
 
 turns into
@@ -1587,14 +1596,52 @@
 
 If not used, they are not added, and a dispatch with mispatched parameters 
will fail.
 
-Placeholders may also be used in method bodies that have no formal signature.
+The use of a P5ish C<@_> in a signatureless sub falls naturally out of this:
 
+    sub sayall { .say for @_ }
+
+Note that in this case, C<$_> is not treated as a placeholder because
+there is already the C<@_> placeholder.  And C<@_> is a placeholder
+only because the sub has no official signature.  Otherwise it would
+be illegal (unless explicitly declared).
+
+Placeholders may also be used in method bodies that have no
+formal signature.  The invocant is always removed first, so the
+first placeholder argument always refers to the first non-invocant
+argument.  C<@_> will never contain the invocant.  The invocant is
+always available via C<self>, of course.
+
 Since the placeholder declares a parameter variable without the twigil,
 the twigil is needed only on the first occurrence of the variable within
 the block.  Subsequent mentions of that variable may omit the twigil.
 Within an internal nested block the twigil I<must> be omitted, since
 it would wrongly attach to the inner block.
 
+Note that, unlike in Perl 5, C<@_> may not be used within an inner block to
+refer to the outer block's arguments:
+
+    sub say-or-print {
+        if $SAYIT {
+            say @_;     # WRONG
+        }
+        else {
+            print @_;   # WRONG
+        }
+    }
+
+because this desugars to:
+
+    sub say-or-print {
+        if $SAYIT -> *...@_ {
+            say @_;
+        }
+        else -> *...@_ {
+            print @_;
+        }
+    }
+
+Translators of Perl 5 will need to bear this in mind.
+
 =head1 Properties and traits
 
 Compile-time properties are called "traits". The

Reply via email to