http://www.perlmonks.org?node_id=914996

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

Greetings, monks

I am trying to build a hash, but I am getting the warning "Odd number of elements in anonymous hash", caused by a function that is supplied by a library:

my $hshref = { foo => func(), bar => "baz" };

I can fix it by forcing scalar context, but without that, the hash ends up malformed. My question is: what does that function return, so as to create a malformed hash? How is that even possible?

(Data::Dumper::Dumper(func()) prints nil, unless I force it to scalar, where it prints "undef")

Replies are listed 'Best First'.
Re: mystery function & Odd number of elements in anonymous hash
by Corion (Patriarch) on Jul 17, 2011 at 19:15 UTC

    Then func() returns an empty list. Perl flattens lists, so what you get is

    my $hshref = { foo => bar => "baz" };

    ... which is a list of three elements, hence the warning. You will need to think long and hard about what the return value of func() is supposed to be, and what it should be instead when the original func() call returns an empty list.

      I delved into the source code, and I believe the offending function ends with return wantarray ? @values : $values[0];.

      So hash building is in list context; the fat comma doesn't change a thing. Why not? Are my options to force scalar context, or append "|| 0" for a default value?

        The fat comma is just that, a fat comma. It stringifies the argument on its lefthand side if it is a bareword.

        Personally, I would use something like:

        my $foo = func(); my $hashref = { foo => $foo, bar => $bar };
        Many functions return the empty list or undef as some sort of error condition. Are you sure @values ought to be empty?

        And how did adding a wrapper to force context solve the issue?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: mystery function & Odd number of elements in anonymous hash
by sundialsvc4 (Abbot) on Jul 18, 2011 at 03:41 UTC

    Looking at this thing from a slight distance, I suggest that you might want to do what you want to do using several statements instead of just one, and that you especially want to be sure that the resulting data structure does not depend on precisely what the function does return.   For example, you might want to put the function-results (whatever they turn out to be ...) say, into an arrayref, the element(s) of that arrayref being “whatever the function returned... no matter what it did return.”

    While “one-liners” are sometimes convenient, quite frankly, I sometimes opine that they are more trouble than they’re worth.

      you especially want to be sure that the resulting data structure does not depend on precisely what the function does return

      You're certainly correct, and I will eventually expand that with error checking. Right now, it is a priority for me to create code that works, a prototype. I will revisit it and make it robust later.

      And to JavaFan: No reason other than to save a few characters.