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

a function returning nothing (not undef) in list context;

I don't get this point. I take it to mean the assignment of an empty list to a hash, but none of the circumstances that I can think of that involve such an assignment elicit any warning, nor should they IMHO: an empty hash is a valid hash.

c:\@Work\Perl\monks>perl -wMstrict -le "my %hash; my %hasi = (); my %hasj = empty_list(); ;; print scalar keys %hasj; ;; sub empty_list { return; } " 0
Can you expand a bit on this point?


Give a man a fish:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^2: Why is "odd number of elements in hash assignment" warning and not error?
by Dallaylaen (Chaplain) on Feb 18, 2015 at 18:20 UTC

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

Re^2: Why is "odd number of elements in hash assignment" warning and not error?
by Anonymous Monk on Feb 18, 2015 at 18:06 UTC

    I understood that point to mean one of these two, probably the first:

    sub nothing { return } sub something { return undef } my %foo = ( abc => nothing() ); my %bar = ( abc => 123, something() );