in reply to Why does Perl allow you to return array references to variables in local scope?
A good way to think of it is that, in Perl, everything is "a reference-counted Thing," allocated from the heap not from a stack. Anything with a reference-count of zero is eligible for cleanup, although it doesn't necessarily happen right away. When any variable anywhere refers to that Thing, it adds one to the count. When the value changes, the counts are adjusted.
These "things" can be of many intrinsic types .. string, float, integer, file .. or a truly-interesting type: "a reference to something else." In other words, indirection, but without the silly punctuation-marks that a language like C demands. The presence of a reference to any Thing also adds one to the count.
The only thing that you've got to watch-out for in that system is circular references, which of course would keep the counts from ever becoming zero hence the memory would never be freed. Perl has a "weakened reference" feature (Scalar::Util) to deal with that.
It is fast, it is efficient, and it works great. You really get used to it very quickly.
|
|---|