in reply to How are CAM::PDF objects destroyed?

Elaborating a little on afoken's comment: Perl uses reference counted memory management. Memory allocated for an instance of a variable is released when the variable goes out of scope and nothing references it. For the simple case of a local variable the memory is released at then end of the block containing the variable. The more complicated case is where the variable is referenced by another variable. Consider:

do { my $outer; do { my $inner1; # $inner1 memory is allocated }; # $inner1 memory is released do { my $inner2; # $inner2 memory is allocated $outer = \$inner2; # $outer now references $inner2's memory }; # $inner2 goes out of scope but isn't released - referenced by +$outer }; # $outer goes out of scope and is released which also releases memo +ry for $inner2

There are some subtle cases such as closures and circular references which complicate things, but in general Perl simply does what you want, even when you don't know you want it.

Premature optimization is the root of all job security