Values & References18 / 18
Up next

Break a cycle with weaken

use strict;
use warnings;
use feature 'say';
use Scalar::Util qw(weaken);

# a parent/child cycle would leak memory
my $parent = { name => "p" };
my $child  = { name => "c" };
$parent->{child}  = $child;
$child->{parent}  = $parent;
# weaken the back-reference so the cycle can be freed
weaken($child->{parent});
say $child->{parent}{name};
What you're solving

Scalar::Util weaken avoids leaks