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

RFE: Multiple-alias syntax for foreach

Thread Next
Dan Book
March 31, 2017 22:40
RFE: Multiple-alias syntax for foreach
Message ID:
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

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



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