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

Dear Monks,

I would appreciate if someone could tell me what this warning means: "Reference found where even-sized list expected at filename.pl line 16"
Line 16 looks like: my %gGui = {};
I use it like this and so far I have not noticed anything wrong, but I would like to improve if I can:
my $dest=""; $gGui{dest} = $dest; $gGui{single} = 0;
In case it makes a difference, I work with Perl/tk.
Thanks,
Claire

Replies are listed 'Best First'.
Re: Hash/Associative Array
by davido (Cardinal) on Aug 25, 2005 at 05:13 UTC

    %gGui is a proper hash. {} returns a hashref to an empty anonymous hash. You're assigning a hashref to a proper hash. A hashref is a single entity; the reference. Anytime you assign to a hash as "%hash = whatever" whatever has to be a list, or you'll get a warning indicating that you're probably on the wrong track. ;)

    You may find further enlightenment in perlintro, and perlref.

    By the way, 'kudos' to you for running with use warnings;, and for not just turning them off. Every warning is there for a reason, and whenever you don't understand the reason, it's a good idea to investigate further rather than just turning them off. You are on the right track already...


    Dave

      Thank you Dave for the encouragement--I sure need some from time to time and this is a good time for it!. I would like to know if you would do something like this?:
      my %gGui = {}; $gGui{source} = (my $source,"");
      I have done this for one of my variable, but I have hundred of them, and before I change all my code, I would like to know... I'm sure you'll understand!
      Thanks again,
      Looking forward to hearing from you,
      Claire

        Same mistake: my %hGui = {};. Again, that's mistakenly assigning a hashref to an actual hash. It's a juxtaposition of data types. A %hash expects a list. A {} returns a reference, not a list. That's where you're getting the warning you asked about.

        Also, $gGui{source} = ( my $source, "" ) is another problem. $gGui{source} expects a scalar value, not a list. You're trying to provide it with a list. The first element of the list would be undef, because it is the return value from my $source, which since $source isn't asigned a value, is an undefined value. The second element of the list is "" (an empty string). But since $gGui{source} provides scalar context to the expression, there's no list; just the rightmost element in what would have been a list; the empty string, "". Confusing? Yes, but it doesn't have to be.

        The specifics of the comma operator in scalar context are discussed in perlop. But you're missing the bigger picture here somehow.

        Hashes (the datatype) hold a list of scalars, indexed by hash keys. If you assign a list of two elements to a hash, the first element will be taken as a hash key, and the second as a hash value. Ten elements will be five keys and five values. An odd number of elements will throw a warning.

        Hash elements accept scalar values. Scalar values can be a lot of things such as numbers, strings, undef, or references to other scalars, hashes, arrays, or whatever.

        Please do read through perlintro. Follow that by perldata, and then follow up with perlref, perlreftut, and perllol. We're talking about a few hours of reading here, but you'll find yourself greatly enlightened on some concepts that may seem a little complicated at first.


        Dave

Re: Hash/Associative Array
by ysth (Canon) on Aug 25, 2005 at 06:25 UTC
    If you want to declare a hash, just use my %hash;; it starts off empty. Some people are obsessive about initializing things and say my %hash = ();; this is not necessary, but if it makes you feel better, go ahead. my %hash = {}; does something quite different; it declares a hash and sets one key to something like HASH(0xDEAF0) with a value of undef. This is almost certainly not what you want, and the {} was meant as (), so perl warns you.
Re: Hash/Associative Array
by sk (Curate) on Aug 25, 2005 at 05:36 UTC
    make that my %gGui = ();