Given this code:
#!/usr/bin/perl -w
use strict;
use warnings;
sub foo {
return wantarray ? () : "value1";
}
my $hash = {
key1 => foo(),
key2 => 'value2'
};
use Data::Dumper;
print Dumper($hash);
I get the following output:
$VAR1 = {
'key1' => 'key2',
'value2' => undef
};
When I would expect:
$VAR1 = {
'key1' => 'value1',
'key2' => 'value2'
};
I understand that a hash is kind-of an even-sized array (as evidenced by the "Odd number of elements in hash assignment" warning I'm getting) but a hash element can only be a scalar, why would the compiler be giving it array context?
I found this using the param function of the CGI module when assigning directly to a hash. The foo() function above was a call to CGI::param('mistyped_url_param') which was returning an empty array, destroying (rotating?) the hash structure.