Father Chrysostomos pointed out:

> I said when, not whether. :-)

Isn't that just typical of me: confusing ontology with chronology. ;-)

I'm afraid don't know the implementation details for Rakudo. It may be
bound as the surrounding block is entered, or perhaps just-in-time when
the Code object is first accessed in some way.


> Does Perl 6 have an equivalent to this?
>
>     my $x;
>     for $x(1..10) {}

In Perl 6 that's:

    my $x;
    for 1..10 -> $x {}

And, as in Perl 5, they're two separate variables.

The iterator $x is a parameter of the loop block and acts as a readonly
alias to each successive iterated value, unless you write:

    for 1..10 <-> $x {}

in which case it's a read-write alias to each iterated value.

Loop iterators are never package-scoped in Perl 6.


>  is just a syntactic convenience for:
>>
>>         my &foo := sub { whatever() }
>
> Except that my sub foo happens upon block entry, right?

Quite so. So the equivalence is more precisely:

    ENTER my &foo := sub { whatever() };

except that the named code object is visible throughout its surrounding
block (even before its declaration point).

So I guess that equivalence I keep referring to is a little shakey. ;-)


> In Perl 5, $] in a piece of code is bound to *], not $], so it sees
> changes made by local($]) (which actually puts a completely new scalar
> in the *]{SCALAR} slot).  But ‘my $x; sub { $x }’ is bound, not to the
> $x slot in the outer block/sub/file, but to the actual scalar itself.
>
> It seems that Perl 6 closures close over the slot, not the
> scalar/array/etc.  Is that right?

That's right. This:

    my $x       = 1;
    my $x_prime = -1;

    sub foo { say $x }

    foo();

    $x := $x_prime;

    foo();

prints:

    1
    -1

...indicating that the $x in foo is bound to the slot, not to any var
implementation currently in the slot. The way I would have described it
is that binding is by name, not by reference.

Damian

Reply via email to