Re: References to hash elements?

[email protected] (Jeff Clites)
Newsgroups perl.perl6.internals,perl.ponie.dev
Message-ID <[email protected]>
[ apologies for the duplicate email message from my last post--mail 
client problems... ]

On Jan 6, 2004, at 11:50 AM, Simon Cozens wrote:

> Jeff Clites:
>> $a = $hash{bar};
>
> Here you used the copy constructor before taking the reference. It 
> might look
> like an assignment operator, but it isn't. You're better off thinking 
> that
> assignment doesn't exist. It's a copy constructor. It makes the PMC 
> referred
> to by $a a copy of the PMC in $hash{bar}. Their values may be "equal" 
> but
> they're two different PMCs.

But here what I'm copying is the _contents_ of the hash slot--so I'm 
copying a PerlString PMC, for instance.

>> $b = \$hash{bar};
>
> Here you didn't make a copy before taking the reference. No copy, only 
> one
> PMC. It all works.

And here I'm not making a copy, but also the thing I'm taking a 
reference to is not the same thing I copied above. Here, it's a 
reference to a hash slot.


So maybe the way to think about it is:

$a = $hash{bar};
#This finds the hash slot, pulls out its contents, and copies that 
contents into $a.

$b = \$hash{bar};
#This finds the hash slot, takes a reference to it, and places that 
reference in $b.

So the first case involves copying a string, and the second involves 
creating a reference to a hash slot. In particular, if the hash is 
initially empty, the first case should end up with undef in $a (so 
nothing actually copied, if undef is a singleton), but $b would 
(presumably) still contain a real reference (to a hash slot which 
doesn't contain anything yet), which would create a hash entry if you 
"$$b = 'goo'". And that reference-to-a-hash-slot is probably a separate 
special PMC (it needs to keep track of the hash and key, and work even 
if the hash is empty).

JEff
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.