On Wed, Feb 14, 2001 at 06:33:00PM -0500, Michael S. Richey wrote:
> Alright this one has had me stumped to long.
> I've been searching through "Programming Perl (3rd ed.)," but
> have had no luck. It seems like my references are getting
> converted to strings against my wishes. Can someone
> tell me where and why this is happening and how to stop/avoid it?
> # the references are unique, use them as keys
> my $wordref = \$word;
> my $numref = \$num;
>
> # create hash
> my %hash = (
> $wordref => "one",
> $numref => "two"
> );
>
perldoc perlref:
WARNING
You may not (usefully) use a reference as the key to a
hash. It will be converted into a string:
$x{ \$a } = $a;
If you try to dereference the key, it won't do a hard
dereference, and you won't accomplish what you're
attempting. You might want to do something more like
$r = \@a;
$x{ $r } = $r;
And then at least you can use the values(), which will be
real refs, instead of the keys(), which won't.
The standard Tie::RefHash module provides a convenient
workaround to this.
Since this is in perlref, I expect it is also somewhere in Programming
Perl. I don't have the 3rd edition at hand at the moment.
Ronald