Storing references in a hash - why does Perl complain here?

R

Ronny

I would like to build a hash, where the keys are strings, and the
values are
references (actually, they are references to anonymous hashes). When I
start like this:

my %uts={};
$uts{X}={};

Perl 5.8.6 complains:

"Reference found where even-sized list expected"

Now I don't question the *content* of this sentence (actually, {} *is*
a reference), but I wonder
why Perl insists in having something like

$uts{X}=();

instead. What is wrong in storing a hash reference in a hash? Other
references (I tried references
to numbers, references to arrays, and code references) work fine.

Ronald
 
1

1usa

I would like to build a hash, where the keys are strings, and the
values are
references (actually, they are references to anonymous hashes). When I
start like this:

my %uts={};
$uts{X}={};

Perl 5.8.6 complains:

"Reference found where even-sized list expected"

That is not the whole error message. It is important to read all that
perl tells you: It should have given you a line number.

perl is complaining about
my %uts={};

That should have been:

my %uts = ();

But, then, you really do not need the initializer, so

my %uts;

would have been better.

Sinan
 
T

Tad J McClellan

Ronny said:
I would like to build a hash, where the keys are strings,


Hash keys are *always* strings.

and the
values are
references (actually, they are references to anonymous hashes). When I
start like this:

my %uts={};
$uts{X}={};

Perl 5.8.6 complains:

"Reference found where even-sized list expected"


Did the complaint include a line number?

Did you pay close attention to which line the message was
referring to?

It is complaining about the 1st line, not the 2nd one.

Now I don't question the *content* of this sentence (actually, {} *is*
a reference), but I wonder
why Perl insists in having something like

$uts{X}=();

instead.


That is not what it is insisting on at all, rather it is insisting
on something like:

my %uts=()
or
my %uts;

instead.

What is wrong in storing a hash reference in a hash?


Nothing.

What is wrong with storing a hash reference as a hash _key_?

Is a question that applies to your code...

Other
references (I tried references
to numbers, references to arrays, and code references) work fine.


If you had shown us your code using those other types of references,
then we could have explained how and why they are different.

But you didn't, so we can't.
 
R

Ronny

It's not complaining about that, it's complaining about the line before it,
which should be

my %uts = ();

Or simply

my %uts;

Of course, you are right!!!! Thank you for pointing this out!

Ronald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top