develooper Front page | perl.perl5.porters | Postings from November 2016

Re: [perl #130038] Builtin variable for loop index in foreach

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
November 28, 2016 09:34
Subject:
Re: [perl #130038] Builtin variable for loop index in foreach
Message ID:
20161128093446.GO4785@iabyn.com
On Mon, Nov 28, 2016 at 08:53:34AM +0000, Dave Mitchell wrote:
> On Mon, Nov 28, 2016 at 08:14:23PM +1300, Kent Fredric wrote:
> > At parse time, the Perl compiler parsing the for { } block *lexically*
> > parses for a variable ( we'll just say $# for now ).
> > 
> > And then upon detecting that variable's usage in its block, then instruments
> > the for loop to behave differently.
> 
> what about:
> 
>     for (...) {
>         eval '$#';
>     }

Another approach would be to extend the for syntax, e.g.

    for       -> $i    (....) { say "$i: $_" }
    for $x    -> my $i (....) { say "$i: $x" }
    for my $x -> my $i (....) { say "$i: $x" }

for a suitable bikeshedding of '->'.

-- 
Indomitable in retreat, invincible in advance, insufferable in victory
    -- Churchill on Montgomery

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