Recursive Link in Perl

$a=\$a; 

The book I am reading says that in this case $a NEVER be free, my question is why does the perl interpreter not fix it at compile time? When it discovers that it is pointing to itself, do not increase the value of refcount.

Why doesn't perl do this?

+4
source share
1 answer

Some garbage collectors have loop detection; Perl, for performance and historical reasons, no. If you need a link that does not affect the link count, you can use Scalar::Util::weaken to get a weak link, which eliminates the need for loop detection in most situations where you need to rely on it. Loop detection should be detected in the interpreter to automatically determine if \$a should be a weak or strong reference, so you just need to do this explicitly.

+6
source

All Articles