develooper Front page | perl.perl5.porters | Postings from March 2017

RFE: Multiple-alias syntax for foreach

Thread Next
From:
Dan Book
Date:
March 31, 2017 22:40
Subject:
RFE: Multiple-alias syntax for foreach
Message ID:
CABMkAVXc4XkEhb0VN97W9P6Y-nxsJ=SogOkt_W2k_PMA0+9kJg@mail.gmail.com
The "each" function is generally discouraged due to implementation details,
but is still the most "pretty" and concise way to write such a loop:

while (my ($key, $value) = each %hash) { ... }

A possible alternative to this would be a syntax to alias multiple items
per iteration in a for/foreach loop.

foreach my ($key, $value) (%hash) { ... }

This could be generalized as a "bundler", in other words to alias a number
of elements from the list equal to the number of variables specified. Such
as:

foreach my ($foo, $bar, $baz) (@array) { # $foo, $bar, and $baz are the
next three elements of @array, or undef if overflowed

I can think of one syntactical issue presently: "foreach ($foo, $bar)"
without the "my" is already valid syntax so would be difficult to
disambiguate without looking for the second () denoting the list to iterate
through.

Thoughts?

-Dan

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