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

I've spent today working on better understanding how complex data structures work in Perl and I've come up with a question.

If you do the following..

my $ref = CreateRef(); sub CreateRef { my %some_hash = ('foo' => 'bar'); return \%some_hash; }
Now is it safe to use what's at $ref? I've used Data::Dumper to view data refrenced by a returned refrence but just because it's there now, doesn't mean that it's going to be there later. So, can anyone share with me the rules about when data becomes "freed"?

Thanks,
Rich

Replies are listed 'Best First'.
Re: Variables living outside their scope
by merlyn (Sage) on May 01, 2001 at 05:30 UTC
    It's freed when the refcount goes to zero. Your code is perfectly safe, and is reminiscint of how we create objects every day.

    -- Randal L. Schwartz, Perl hacker

Re: Variables living outside their scope
by jbert (Priest) on May 01, 2001 at 16:31 UTC
    Obviously, Randal is right - but some elucidation:
    • In C, a common beginner error is to return the address of an automatic variable which is deallocated when the function exits. The perl code above 'feels' roughly equivalent to this, presumably hence the question.
    • In perl, as Randal says, variables are reference counted. This is a cool system which means that perl keeps track of whether anything cares about your data and frees it when nothing wants it any more.
      sub another_func { my $ref = CreateRef(); # do things with $ref return 1; # $ref goes out of scope here and so we lose our last # reference to the hash and it is freed. } sub CreateRef { my %some_hash = ('foo' => 'bar'); return \%some_hash; # We lose the 'some_hash' name reference to # the 'some_hash' data, but we have created another # reference (the return value) so the data lives... }
    • In perl you can have references to anonymous arrays and hashes. To C coders, this 'feels' like memory allocation, but to perl it doesn't really make any difference to how your memory is freed. (It just means that the data doesn't have a reference to it by virtue of having a name).
      # Note use of {} (same as for accessing hash elem) my $ref_to_anon_hash = { foo => 'bar' }; # Note use of () (same as for accessing array elem) my $ref_to_anon_array = [ 1, 2 ];
    • Reference counting (as used in perl) has one weakness though. It does not take account of loops of references. In this case, you will have a memory leak.
      sub leak_mem { my $foo; # foo data has one reference, the 'foo' name my $bar; # bar data has one reference, the 'bar' name $foo = \$bar; # bar data has two refs $bar = \$foo; # foo data has two refs return 1; # Oops. foo and bar data reference count are both # decremented by one (because the foo and bar names have # just gone out of scope), but that doesn't take them # to zero. # We just lost some bytes, sir. }
      Thank you jbert, that was an excellent reply. It helped me greatly. I hope rchiav was satisfied with the answear.

      -= Ozzy =-
Re: Variables living outside their scope
by Dominus (Parson) on May 01, 2001 at 19:26 UTC
    Says rchiav:
    can anyone share with me the rules about when data becomes "freed"?
    This node may also be helpful. It discusses the difference between 'scope' (the part of a program in which a variable name is visible) and 'duration' (the time during the program's execution during which a value exists.)

    --
    Mark Dominus
    Perl Paraphernalia