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:
Kent Fredric
Date:
November 28, 2016 09:21
Subject:
Re: [perl #130038] Builtin variable for loop index in foreach
Message ID:
CAATnKFA2msj6FALvLW7_Wd6qmANinMDJ1CdRqougWcyn8N1O_A@mail.gmail.com
On 28 November 2016 at 21:53, Dave Mitchell <davem@iabyn.com> wrote:
> what about:
>
>     for (...) {
>         eval '$#';
>     }


Yeah. That's the annoyingest part of this idea. I don't know how to
make that seem unweird.

If we take a different tack and propose, instead of some arcane magic,
that we define a new keyword,

> for_idx

That can take *two* variables:

for_idx my $key, $value ( @array ) {
    ...
}

Then we get the benefit of explicitness, the benefit of clarity, and
the benefit of
working even in evals.

Theoretically, this idea could even be prototyped on CPAN.

If anyone goes in this direction, may I also tempt them with:

   for_assoc my $a, $b, $c ( @array ) {

   }

Which would be a "more perlish" way of the sort of idea people are trying to
get over in the signature introspection thread, and it would be really
awesome if
that gave us something like

    for_assoc my $key, $value ( %hash ) {

   }

#justsayin

But as you can see, this is kinda bikeshed territory: Its clearly the
sort of thing we all hate bumping into all the time as
a feature that perls missing, but its not really that clear how we
should implement it sanely. So CPAN prototypes
would be really nice to prove which strategy is the nicest for Perl.

I'm not in any hurry to shoehorn one of these proposals into Perl wrongly. :)



-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

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