First, a very simple example of a closure:
{
my $tree;
sub get_tree {
return $tree;
};
};
In the above example, $tree and all memory referenced by it will never go out of scope, and since $tree is invisible outside of its containing block, there is no way to free up the referenced memory.
{
my $tree;
sub get_tree {
return $tree;
};
sub release_tree {
undef $tree;
};
};
In this example, it is possible to release the memory structure referenced by $tree by calling release_tree, because it also sees the $tree variable. But this release is not automatic and thus must be done by the programmer when the time has come.
|