Hi all,

The perlref docs state "Hard references are smart--they keep track of
reference counts for you, automatically freeing the thing referred to when
its reference count goes to zero."  My interpretation of this is that when a
reference goes out of scope the memory used by the referent is freed.  For
example:

foreach my $term ( @list )
{
        my $hashref = makehash( $term );

        # do stuff with $hashref
}

sub makehash
{
        my ( $term ) = @_;

        my %hash = ( term => $term, state => 0 );

        return( \%hash );
}

While %hash goes out of scope at the end of 'makehash', the reference to it
that is returned keeps the referent around and accessible.  Once the
reference ($hashref) goes out of scope at the end of the foreach loop,
however, the memory used by the referent freed.  (Or am I not understanding
this correctly?)

My question is this:  what would happen if the foreach loop was replaced with
the following?

foreach my $term ( @list )
{
        my $hashref = makehash( $term );

        # do stuff with $hashref

        $term = $term . '2';

        $hashref = makehash( $term );

        # do more stuff with $hashref
}

When the value of $hashref is changed to point to the new hash ('term2'), is
the memory used by the old hash ('term') freed?  If not, it seems like that
block of memory would remain in use for the remainder of the program, but it
would not be accessable after the reassignment of $hashref.

If that's true, how do I do the garbage collection manually (I've read
perlobj but don't entirely understand it)?

TIA,

Bob




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to