Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Update references

by stevieb (Chaplain)
on May 13, 2012 at 20:08 UTC ( #970326=note: print w/ replies, xml ) Need Help??


in reply to Update references

Can anyone explain *why* this is happening? I don't know enough of the perl innards to grasp it.

When redefining @a, does Perl actually create a new array at a different memory location, leaving the old existing element simply because a reference still points to it?


Comment on Re: Update references
Re^2: Update references
by Anonymous Monk on May 13, 2012 at 21:00 UTC

    I read in O'Reilly's Intermediate Perl Sec 5.2 that data stays alive until the last reference to it is destroyed.

Re^2: Update references
by Eliya (Vicar) on May 13, 2012 at 21:00 UTC

    The reference refers to the scalar retrieved via $a[0], not the array index [0].  The reference knows nothing about the array that scalar came from.  And the scalar will exist as long as its refcount is > 0.

    $ perl -MDevel::Peek -E'@a=qw(a b c); $r=\$a[0]; Dump $r; @a=(); Dump +$r' SV = IV(0xa95bc0) at 0xa95bd0 REFCNT = 1 FLAGS = (ROK) RV = 0xa77998 SV = PV(0xa75c20) at 0xa77998 REFCNT = 2 <--- FLAGS = (POK,pPOK) PV = 0xa8ee40 "a"\0 CUR = 1 LEN = 16 SV = IV(0xa95bc0) at 0xa95bd0 REFCNT = 1 FLAGS = (ROK) RV = 0xa77998 SV = PV(0xa75c20) at 0xa77998 REFCNT = 1 <--- FLAGS = (POK,pPOK) PV = 0xa8ee40 "a"\0 CUR = 1 LEN = 16

    (note the refcounts)

      That's what I was after, and it makes complete sense. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://970326]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-12-26 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (173 votes), past polls