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... } #### # 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 ]; #### 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. }