I am trying to write a daemon with perl. Now this daemon has the following code

sub b {
    my $data;
    if (some condition) {
         $data->{"endsmeet"} = 1;
    } else {
        $data->{"endsmeet"} = 2;
    }

    my $newData = a($data);
}

sub a {
    my ($data) = @_;
    my %a = ();
    my $newData = {
      endsmeet => undef,
    };
    $a{"boo"} = $data->{"endsmeet"};
    $newData->{"endsmeet"} = \%a;
    return $newData;
}

My question is from the above, does the reference for %a go away and does it get cleaned up when b goes out of scope?

有帮助吗?

解决方案

b returns the value of $newdata, which is a reference to an anon hash, which holds a reference to %a, which holds a scalar in the element with key boo.

If the value returned by b not stored, nothing will be referencing the value of $newdata, so it will get freed, so nothing will be referencing the anon hash, so it will get freed, so nothing will reference the scalar in the element with key boo, so it will get freed.

No cycles. No leak.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top