in reply to Re: Why is "odd number of elements in hash assignment" warning and not error?
in thread Why is "odd number of elements in hash assignment" warning and not error?

Sure. I should've given examples to begin with... Here's a sample:

use strict; use warnings; use Data::Dumper; my %hash = (0 => foo(0), 3 => foo(3), 7 => foo(7)); print Dumper(\%hash); sub foo { return unless $_[0]; return $_[0] * 2; };

Now what one probably expected is (0 => undef, 3=>6, 7=>14). However, the real output would be (0 => 3, 6 => 7, 14 => undef) which is kinda weird. This is because hash assignment forces list context, so return; returns empty list which wreaks havoc on the rest of the hash. It can be fixed by arbitrary scalar foo(...) or by returning undef explicitly (perlcritic would complain about it though).

  • Comment on Re^2: Why is "odd number of elements in hash assignment" warning and not error?
  • Download Code