interesting "feature": \%$x == $x

T

Ted Zlatanov

perl -e'$x = {}; print "%s == %s\n", $x, \%$x'

-> HASH(0xfc7d48) == HASH(0xfc7d48)

I wonder if this is a bug or a feature.

Ted
 
U

Uri Guttman

TZ> perl -e'$x = {}; print "%s == %s\n", $x, \%$x'

-> HASH(0xfc7d48) == HASH(0xfc7d48)

perl -e'$x = {}; print "%s == %s\n", $x, \%$x'
%s == %s
HASH(0x1271df0)HASH(0x1271df0)

did you mean printf?

TZ> I wonder if this is a bug or a feature.

hard to tell as you don't say what you think went wrong.

if it was printf you meant, it makes perfect sense. you dereferenced $x
and took a reference. it should give the same address. it isn't copying
the hash and allocating a new one like an anon hash would. these are
very different:

\%$x
{%$x}

uri
 
T

Ted Zlatanov

BM> The numbers in the stringification of the ref are related to the
BM> referent, not the ref itself.

Oh, I see. It all makes sense now. So I was making a new reference to
the same data structure.

BM> If you want a ref to a copy of the hash, you need

BM> my $copy = {%$x};

Yeah, no more "hey I can save a byte" cuteness for me.

Ted
 

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
474,214
Messages
2,571,112
Members
47,704
Latest member
DavidSuita

Latest Threads

Top