-
Notifications
You must be signed in to change notification settings - Fork 580
n-at-a-time foreach
does not print a warning about odd-sized list
#20760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Previous discussion about what the behavior should be: #18925 (comment) When I was writing the RFC for map/grep with multiple variables, I forgot to check it against what the current behavior of for was. I ended up writing that if there were insufficient elements for the final iteration, a warning should be issued. Depending on what gets decided here, I'll be updating the RFC to match. But throwing warnings for this does seem reasonable. |
On Wed, Feb 01, 2023 at 02:53:27PM -0800, Paul Evans wrote:
use experimental 'for_list'; foreach my ($k, $v) (1,2,3) {}
# no warning
```
Perhaps it should? It can't be worded quite the same way, because "odd vs even" is only a useful concept for n=2. But perhaps some wording such as:
I don't think it should warn. Having the list not an exact multiple of the
number of variables has documented behaviour, and in many circumstances,
that will be reasonable behaviour.
The code will already get a warning from subsequently using the undefined
value as-is:
$ perl -we'for my ($x,$y) (1,2,3) { $t += $y }'
...
Use of uninitialized value in addition (+) at -e line 1.
…--
Counsellor Troi states something other than the blindingly obvious.
-- Things That Never Happen in "Star Trek" #16
|
Isn't that also true of |
Yes, but it seems much more likely and useful to intentionally use an uneven list in this case than in hash assignment (particularly since this isn't only for 2 at a time) |
I don't think this should warn, just as |
This is closable as WONTFIX I think, based on the discussion on the list (including by @leonerd who opened the ticket.) |
Since April, no one has called for keeping this ticket open. Closing now. |
Uh oh!
There was an error while loading. Please reload this page.
When assigning to a hash, an even-sized list is required. If an odd-sized list is presented, perl prints a warning:
The new n-at-a-time
foreach
syntax never gives a warning in similar circumstances:Perhaps it should? It can't be worded quite the same way, because "odd vs even" is only a useful concept for n=2. But perhaps some wording such as:
The text was updated successfully, but these errors were encountered: