What exactly are the weak references that KiokuDB tutorial mentions?
How do they differ from ‘normal’ references?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A normal reference prevents the thing being referred to from being garbage collected. A weak reference is like a normal reference, but does not prevent garbage collection. When the last normal reference to an entity is removed, it gets garbage collected, and any weak references to it become
undef.This is useful if you have circular references. A reference-count garbage collector (like Perl uses) cannot remove things with circular references, because their reference count never goes to 0.
For example, consider a tree structure, where parent nodes have references to their child nodes, and child nodes have a reference to their parent. By making the child-to-parent references weak, the tree will be automatically garbage collected when there are no external references to it.
In Perl, weak references can be created with the
weakenfunction in Scalar::Util. Moose also allows you to mark attributes as weak_ref.