On Sat, Jan 28, 2006 at 06:30:06PM +0100, dakkar wrote:
: Today I was reading S06 from
: http://dev.perl.org/perl6/doc/design/syn/S06.html, and I have some
: perplexities on what's written there.
: 1) it's written:
:     $pair = :when<now>;
:     doit $pair,1,2,3;   # always a positional arg
:     doit *$pair,1,2,3;  # always a named arg
:    but also:
:     To pass pairs out of hash without their being interpreted as named
: parameters, use
:       doit %hash<a>:p,1,2,3;
:       doit %hash{'b'}:p,1,2,3;
:     instead.
:    I interpret the last sentence as meaning that:
:      $hash<a>=:a<2>; doit %hash<a>;
:    would be equivalent to:
:      doit :a<2>
:    which contradicts what is written earlier... I'd assume I'd have to
: write:
:       doit *(%hash<a>);
:    for the pair to be used as a named argument. What am I missing?

You're reading "pass pairs" as meaning to return a particular value
that happens to be a pair.  That's not the purpose of :p, since that's
what happens anyway by default.  You use :p to return both the key
and the value as a pair object from the hash.  If you say:

    %hash<b>=:a<2>; doit %hash<b>;

it's equivalent to

     doit (:a<2>)

whereas if you say

    %hash<b>=:a<2>; doit %hash<b>:p;

it is equivalent to

     doit (:b(:a<2>))

In neither case is the argument taken as a named argument.
I have rewritten the paragraph as:

    Ordinary hash notation will just pass the value of the hash entry as a
    positional argument regardless of whether it is a pair or not.
    To pass both key and value out of hash as a positional pair, use C<:p>.

        doit %hash<a>:p,1,2,3;
        doit %hash{'b'}:p,1,2,3;

    instead.  (The C<:p> stands for "pairs", not "positional"--the
    C<:p> adverb may be placed on any hash reference to make it mean
    "pairs" instead of "values".)

to try to make it clearer.

: 2) Consider:
:      doit %*hash;   # 1 arg, the global hash
:      doit *%hash;   # n args, the key-value pairs in the hash
:    I think I'll use a lot of C-t while writing Perl6 code...

The * as shorthand for GLOBAL:: is allowed only where it wouldn't be confused
with unary *.  That is, only in declarative contexts or as a twigil.

: 3) What does the second line in:
:       push @array, :a<1>;
:       say *pop(@array);
:    mean? I'd parse it as 'say (*pop)(@array)', that is, call the global
: 'pop' subroutine. But the text around this example seems to imply that
: it is to be parsed as 'say *(pop(@array))', i.e. execute 'pop' and splat
: the result. What gives?

The above rule.  If we meant the global pop we'd say:

    say &*pop(@array);


    say GLOBAL::pop(@array);

Gotto go to lunch--more in a bit.  (Please use new subjects if you want
to discuss any of these subpoints further...)


Reply via email to