in reply to Re^2: Scope of lexical variables in the main script
in thread Scope of lexical variables in the main script

Perl uses reference counting. In your case, inside the block,$tricky and $reference each own one reference to the scalar container that holds the string, "TRICKY". While inside the block, the reference count to the SV that holds the string "TRICKY" is two. After the block exits, $tricky goes out of scope, and the reference count drops by one, to one. As long as $reference remains in scope, the SV (the container for a scalar) holding the string "TRICKY" remains alive.

After $reference passes out of scope, assuming nobody else holds a reference to that scalar, the ref-count drops to zero. Perl regains that memory as available for future use. It's not released back to the OS.

Aren't you glad this isn't simple pointers in C or C++? :)


Dave