Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Some perplexities on S06

0 views
Skip to first unread message

Dakkar

unread,
Jan 28, 2006, 12:30:06 PM1/28/06
to perl6-l...@perl.org
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?

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...

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?

4) It's written:

(Conjectural: Within the body you may also use exists on the
parameter name to determine whether it was passed. Maybe this will have
to be restricted to the ? form, unless we're willing to admit that a
parameter could be simultaneously defined and non-existant.)

Ok, this is 'conjectural', but... if i don't pass a parameter in an
invocation, and that parameter had a defined default, that it would be
both defined and non-passed... here I'm sure I'm missing something

5) while talking about pipes, it says that

(0..2; 'a'..'c') ==> my @;tmp;
for @;tmp.zip { say }

produces

[0,'a']
[1,'b']

etc. Right. But then it says:

for @;tmp.each -> $i, $a { say "$i: $a" }

as far as I understand, this should say

0: undef
'a': undef

etc., since 'each' is visiting the multidimensional array 'by rows',
and producing 1 value at a time, which when bound to the 2-ary signature
would leave the $a parameter without a value, and so undef (I'm assuming
that pointy sub positional parameters are implicitly optional, otherwise
that statement would be a run-time error). I'm pretty sure I don't know
what 'each' does...

6) It's written:

Every lexical scope gets its own implicitly declared @; variable,
which is the default receiver.

To me, that is a variable with a null name ('@;' being a twigil).
Should it not be @;_ ?

I'll continue reading...

--
Dakkar - <Mobilis in mobile>
GPG public key fingerprint = A071 E618 DD2C 5901 9574
6FE2 40EA 9883 7519 3F88
key id = 0x75193F88

signature.asc

Larry Wall

unread,
Jan 30, 2006, 3:41:50 PM1/30/06
to perl6-l...@perl.org
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);

or

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...)

Larry

Larry Wall

unread,
Jan 30, 2006, 5:12:51 PM1/30/06
to perl6-l...@perl.org
On Sat, Jan 28, 2006 at 06:30:06PM +0100, dakkar wrote:
: 4) It's written:

:
: (Conjectural: Within the body you may also use exists on the
: parameter name to determine whether it was passed. Maybe this will have
: to be restricted to the ? form, unless we're willing to admit that a
: parameter could be simultaneously defined and non-existant.)
:
: Ok, this is 'conjectural', but... if i don't pass a parameter in an
: invocation, and that parameter had a defined default, that it would be
: both defined and non-passed... here I'm sure I'm missing something

That's precisely the point. It's just a bit strange to have something
that is defined but doesn't "exist".

: 5) while talking about pipes, it says that


:
: (0..2; 'a'..'c') ==> my @;tmp;
: for @;tmp.zip { say }
:
: produces
:
: [0,'a']
: [1,'b']
:
: etc. Right. But then it says:
:
: for @;tmp.each -> $i, $a { say "$i: $a" }
:
: as far as I understand, this should say
:
: 0: undef
: 'a': undef
:
: etc., since 'each' is visiting the multidimensional array 'by rows',
: and producing 1 value at a time, which when bound to the 2-ary signature
: would leave the $a parameter without a value, and so undef (I'm assuming
: that pointy sub positional parameters are implicitly optional, otherwise
: that statement would be a run-time error). I'm pretty sure I don't know
: what 'each' does...

The "each" is just producing a sequence of values (without bundling
them up into tuple objects like "zip"). It's the "for" that is
deciding to take these list values 2 at a time because of the arity
of the block's signature. That is why, if you use "zip", you have
to put the args as [$i, $a] in order to present an arity of 1 to "for".

: 6) It's written:


:
: Every lexical scope gets its own implicitly declared @; variable,
: which is the default receiver.
:
: To me, that is a variable with a null name ('@;' being a twigil).
: Should it not be @;_ ?

I suppose there's an argument for that. We certainly haven't hesitated
(much) to define $! differently from $!foo, for instance. And Perl 5
distinguishes $#foo from $# (that latter being deprecated, of course).
On the other hand there's no reason in principle that a twigil *has*
to mean something entirely different just because it has a null name.
Plus I think people would rebel if all "underlying" objects had to have
an underline. We'd end up with $/_ instead of $/, and $!_ instead of $!,
and would $!_ then be misinterpreted as a private attribute named "_"?

Larry

0 new messages