perlabc has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a piece fo code which gives me the error in the subject. I am trying to pass a hashref as an argument to a function. This hashref has a key having its value as an anonymous function, which in turn is calling another subroutine. Something like:
foo($dbh, {
    key1 => val1,
    key2 => val2,
    key3 => sub {
       my ($dbh, $subargs) = @_;
       return bar($dbh, { %$subargs, subkey1 => subval1, subkey2 => subval2});
    },
    key4 => val4,
}); Error is because of the value for key3. I am not sure how do I correct this issue. Please help.
  • Comment on Error: Odd number of elements in anonymous hash

Replies are listed 'Best First'.
Re: Error: Odd number of elements in anonymous hash
by ikegami (Patriarch) on Jul 22, 2011 at 22:05 UTC
    Please show us your *actual* code.
Re: Error: Odd number of elements in anonymous hash
by onelesd (Pilgrim) on Jul 23, 2011 at 06:54 UTC
    This does not make sense:
    ... return bar($dbh, { %$subargs, subkey1 => subval1, subkey2 => subval2}) +; ...
    This does:
    ... return bar($dbh, { myhashref => $subargs, subkey1 => subval1, subkey2 +=> subval2}); ...

      It's odd, but %$subargs is valid there. It expands the hash reference, then overwrites the values of subkey1 and subkey2.

        Hmm, auto expansion of the hash reference makes sense, but why would it overwrite subkey1 and subkey2? Can you point me to some documentation regarding that?