Author: larry Date: Tue Feb 5 09:55:29 2008 New Revision: 14501 Modified: doc/trunk/design/syn/S02.pod doc/trunk/design/syn/S03.pod doc/trunk/design/syn/S06.pod
Log: Added named placeholders using $:foo twigil; idea from cognominal++: Placeholder subs can now also autoadd [EMAIL PROTECTED] or *%_ to the signature Methods without signatures may use placeholders Modified: doc/trunk/design/syn/S02.pod ============================================================================== --- doc/trunk/design/syn/S02.pod (original) +++ doc/trunk/design/syn/S02.pod Tue Feb 5 09:55:29 2008 @@ -12,9 +12,9 @@ Maintainer: Larry Wall <[EMAIL PROTECTED]> Date: 10 Aug 2004 - Last Modified: 17 Jan 2008 + Last Modified: 5 Feb 2008 Number: 2 - Version: 126 + Version: 127 This document summarizes Apocalypse 2, which covers small-scale lexical items and typological issues. (These Synopses also contain @@ -1292,7 +1292,8 @@ $foo ordinary scoping $.foo object attribute accessor - $^foo self-declared formal parameter + $^foo self-declared formal positional parameter + $:foo self-declared formal named parameter $*foo global variable $+foo contextual variable $?foo compiler hint variable @@ -1583,6 +1584,10 @@ my ($a, $, $c) = 1..3; print unless (state $)++; +Outside of declarative contexts you may use C<*> for a placeholder: + + ($a, *, $c) = 1..3; + =item * Ordinary package-qualified names look like in PerlĀ 5: Modified: doc/trunk/design/syn/S03.pod ============================================================================== --- doc/trunk/design/syn/S03.pod (original) +++ doc/trunk/design/syn/S03.pod Tue Feb 5 09:55:29 2008 @@ -2791,7 +2791,7 @@ are insufficient for defining the "pecking order" of code. Note that you can bind to either a bare block or a pointy block. Binding to a bare block conveniently leaves the topic in C<$_>, so the final form -above is equivalent to a C<default>. (Placeholders parameters may +above is equivalent to a C<default>. (Placeholder parameters may also be used in the bare block form, though of course their types cannot be specified that way.) Modified: doc/trunk/design/syn/S06.pod ============================================================================== --- doc/trunk/design/syn/S06.pod (original) +++ doc/trunk/design/syn/S06.pod Tue Feb 5 09:55:29 2008 @@ -13,9 +13,9 @@ Maintainer: Larry Wall <[EMAIL PROTECTED]> Date: 21 Mar 2003 - Last Modified: 2 Jan 2008 + Last Modified: 5 Feb 2008 Number: 6 - Version: 91 + Version: 92 This document summarizes Apocalypse 6, which covers subroutines and the @@ -151,10 +151,15 @@ means the declared formal parameter is I<not> considered readonly; only its elements are. See L</Parameters and arguments> below. -Note also that if the sub's block contains placeholder variables (such -as C<$^foo>), it is considered to have a formal signature already, so -no implicit bindings to C<@_> or C<%_> are assumed (nor is an explicit -signature allowed.) So in that case, this section does not apply. +Note also that if the sub's block contains placeholder variables +(such as C<$^foo> or C<$:bar>), those are considered to be formal +parameters already, so in that case C<@_> or C<%_> fill the role of +sopping up unmatched arguments. That is, if those containers are +explicitly mentioned within the body, they are added as slurpy +parameters. This allows you to easily customize your error message +on unrecognized parameters. If they are not mentioned in the body, +they are not added to the signature, and normal dispatch rules will +simply fail if the signature cannot be bound. =head2 Blocks @@ -1508,25 +1513,42 @@ Even though every bare block is a closure, bare blocks can't have explicit parameter lists. Instead, they use "placeholder" variables, -marked by a caret (C<^>) after their sigils. +marked by a caret (C<^>) or a colon (C<:>) after their sigils. +The caret marks positional placeholders, while the colon marks named +placeholders. Using placeholders in a block defines an implicit parameter list. The -signature is the list of distinct placeholder names, sorted in Unicode order. -So: +signature is the list of distinct positional placeholder names, +sorted in Unicode order, following by the named placeholder names in +any order. So: - { $^y < $^z && $^x != 2 } + { say "woof" if $:dog; $^y < $^z && $^x != 2 } is a shorthand for: - -> $x,$y,$z { $y < $z && $x != 2 } + -> $x,$y,$z,:$dog { say "woof" if $dog; $y < $z && $x != 2 } Note that placeholder variables syntactically cannot have type constraints. Also, it is illegal to use placeholder variables in a block that already has a signature, because the autogenerated signature would conflict with that. -Placeholder names consisting of a single uppercase letter are disallowed, +Positional placeholder names consisting of a single uppercase letter are disallowed, not because we're mean, but because it helps us catch references to obsolete PerlĀ 5 variables such as $^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: + + { say $:what; warn "bad option: $_\n" for keys %_; } + +turns into + + -> :$what, *%_ { say $what; warn "bad option: $_\n" for keys %_; } + +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. + =head1 Properties and traits Compile-time properties are called "traits". The