Mailing List Archive

hash references broken
print "FOO: " . $A->{$pod} . "\n";

prints:

FOO: HASH(0x10e640)

---

but:

print "FOO: " . (keys $A->{$pod}) . "\n";

dies w/:

Type of arg 1 to keys must be hash (not associative array elem) at
./pod2html line 106, near "}) "

...
why?
Re: hash references broken [ In reply to ]
Excerpts from the mail message of Bill Bumgarner:
)
) print "FOO: " . $A->{$pod} . "\n";
[...]
) FOO: HASH(0x10e640)
[...]
) print "FOO: " . (keys $A->{$pod}) . "\n";
[...]
) Type of arg 1 to keys must be hash (not associative array elem) at
) ./pod2html line 106, near "}) "

$A->{$pod} is an associative array element containing a
_reference_ to a hash and keys() needs a hash (not a reference
to one), so add %{} to yield:

print "FOO: " . (keys %{$A->{$pod}}) . "\n";

Extending keys() to also accept references to hashes may make
sense, but I haven't thought too much about it. I could see
arguments against this as accidentally typing keys($hash)
when you meant hash(%hash) might become less obvious.
--
Tye McQueen tye@metronet.com || tye@doober.usu.edu
Nothing is obvious unless you are overlooking something
http://www.metronet.com/~tye/ (scripts, links, nothing fancy)
Re: hash references broken [ In reply to ]
> print "FOO: " . $A->{$pod} . "\n";

> prints:

> FOO: HASH(0x10e640)


ok.

> but:
> print "FOO: " . (keys $A->{$pod}) . "\n";
> dies w/:

> Type of arg 1 to keys must be hash (not associative array elem) at
> ./pod2html line 106, near "}) "

> ...
> why?


because a thing is not a pointer to a thing. a hash is not a hash
reference. $A->{$pod} (not to be confused with $A{$pod} if and only
if you should "use strict") is a REFERENCE TO A HASH -- IT IS NOT
A HASH!!

keys() followed by anything except a % is a major mistake. Perl doesn't
do implicit dereferencing. You need

keys %{ $A->{$pod} }

instead.

00tom
Re: hash references broken [ In reply to ]
>From: Bill Bumgarner <bbum@friday.com>
>
>print "FOO: " . $A->{$pod} . "\n";
>
>prints:
>
>FOO: HASH(0x10e640)
>
>---
>
>but:
>
> print "FOO: " . (keys $A->{$pod}) . "\n";
>

keys %{$A->{$pod}}

> dies w/:
>
>Type of arg 1 to keys must be hash (not associative array elem) at
>./pod2html line 106, near "}) "
>
>...
>why?

Reference/Dereference

andreas