in reply to Do subroutine variables get destroyed?

I see where your C/C++ influence gets you a variable called gptr, but in Perl there are no pointers. A better name is gref. So, to elaborate on Tanktalus's reply a little: Perl variables are reference counted and garbage collected, so when you reference %hsh with $gref Perl bumps the reference count on %hsh. %hsh was created with a reference count of 1 and the count is decremented when %hsh goes out of scope. However by the time %hsh goes out of scope the reference count has been incremented due to the reference assignment to $gref so the storage remains in use until $gref either goes out of scope or is assigned a new value.

For the vast majority of Perl scripts you needn't be concerned about memory management and that significant bane of C, invalid memory accesses, essentially can't happen.

Premature optimization is the root of all job security
  • Comment on Re: Do subroutine variables get destroyed?

Replies are listed 'Best First'.
Re^2: Do subroutine variables get destroyed? (Deterministic Destructor References)
by eyepopslikeamosquito (Archbishop) on May 08, 2016 at 16:37 UTC