lleche has asked for the wisdom of the Perl Monks concerning the following question:

CAM::PDF module includes a object creation function

$doc->new($package, $content)

but I could not find in the documentation or in the scripts provided in the module how to 'destroy' the object and release it from memory. I would have to merge a significant number of pdfs and would like to avoid hitting memory issues.

Replies are listed 'Best First'.
Re: How are CAM::PDF objects destroyed?
by GrandFather (Saint) on Dec 13, 2015 at 08:54 UTC

    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
Re: How are CAM::PDF objects destroyed?
by afoken (Chancellor) on Dec 13, 2015 at 08:21 UTC

    Without reading the docs, my guess is that perl handles it. Either by leaving the scope or by assigning something else to the variable containing the object.

    do { my $pdf=CAM::PDF->new(...); $pdf->foo(); $pdf->bar(); }; # object destroyed by perl
    my $pdf=CAM::PDF->new(...); $pdf->foo(); $pdf->bar(); $pdf=''; # object destroyed by perl

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How are CAM::PDF objects destroyed?
by choroba (Cardinal) on Dec 13, 2015 at 10:39 UTC
    undef is the standard Perl way to destroy an object.
    undef $pdf;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: How are CAM::PDF objects destroyed?
by Laurent_R (Canon) on Dec 13, 2015 at 12:21 UTC