Thursday, September 23, 2004

perl error

Reference found where even-sized list expected at ./test.pl line 39.

-- you get this error when you try to initialize a hash like below

%hash_name = {};

from http://linux.about.com/library/cmd/blcmdl1_perl5005delta.htm

Reference found where even-sized list expected
You gave a single reference where Perl was expecting a list with an
even number of elements (for assignment to a hash). This usually means
that you used the anon hash constructor when you meant to use parens.
In any case, a hash requires key/value pairs.

%hash = { one => 1, two => 2, }; # WRONG
%hash = [ qw/ an anon array / ]; # WRONG
%hash = ( one => 1, two => 2, ); # right
%hash = qw( one 1 two 2 ); # also fine


Curly brackets are used to construct anonymous hashes. Their result is
a reference to that hash. So with %f = {} you're trying to assign a
reference (one item) to a hash. However, hashes are initialized with
lists which must have an even number of elements. Hence the warning.
The proper way to initialize an empty hash is with an empty list, which
is why %f = () works.


No comments: