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

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

Thread Previous | Thread Next
Kent Fredric
November 28, 2016 09:21
Re: [perl #130038] Builtin variable for loop index in foreach
Message ID:
On 28 November 2016 at 21:53, Dave Mitchell <> 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 ) {



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



Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About