develooper Front page | perl.perl5.porters | Postings from July 2012

[perl #113930] Lexical subs

Thread Previous | Thread Next
From:
Father Chrysostomos via RT
Date:
July 8, 2012 12:57
Subject:
[perl #113930] Lexical subs
Message ID:
rt-3.6.HEAD-28836-1341777432-84.113930-14-0@perl.org
On Sun Jul 08 02:13:13 2012, thoughtstream wrote:
> 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.

But by using the term ‘variable’, which is ambiguous, you are not
answering my question! :-)

In Perl 5, ‘my $x; for $x(1..10){}’ is only equivalent to ‘my $x; for my
$x(1..10){}’ because we don’t have the := operator, so there is never
any possibility to see the difference, even though things happen
differently underneath.

Does

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

cause the existing name $x to refer temporarily to each of the numbers,
or is a new $x name created?

What does this do?

    my $x;
    my sub f { say $x }
    for 1..10 -> $x { f(); }

-- 

Father Chrysostomos


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About