Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

mystery function & Odd number of elements in anonymous hash

by Anonymous Monk
on Jul 17, 2011 at 19:13 UTC ( #914996=perlquestion: print w/ replies, xml ) Need Help??
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")

Comment on mystery function & Odd number of elements in anonymous hash
Select or Download Code
Re: mystery function & Odd number of elements in anonymous hash
by Corion (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://914996]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-09-22 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (185 votes), past polls