Приглашаем посетить
Короленко (korolenko.lit-info.ru)

Section 5.1.  More Than One Reference to Data

Previous
Table of Contents
Next

5.1. More Than One Reference to Data

Chapter 4 explored how to take a reference to an array @skipper and place it into a new scalar variable:

my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
my $reference_to_skipper = \@skipper;

We can then copy the reference or take additional references, and they'll all refer to the same thing and are interchangeable:

my $second_reference_to_skipper = $reference_to_skipper;
my $third_reference_to_skipper  = \@skipper;

At this point, we have four different ways to access the data contained in @skipper:

@skipper
@$reference_to_skipper
@$second_reference_to_skipper
@$third_reference_to_skipper

Perl tracks how many ways it can access the data through a mechanism called reference counting . The original name counts as one, and each additional reference that we create (including copies of references) also counts as one. The total number of references to the array of provisions is now four.

We can add and remove references as we wish, and as long as the reference count doesn't hit zero, Perl maintains the array in memory and it is still accessible via any of the other access paths. For example, we might have a temporary reference:

check_provisions_list(\@skipper)

When this subroutine executes, Perl creates a fifth reference to the data and copies it into @_ for the subroutine. The subroutine is free to create additional copies of that reference, which Perl notes as needed. Typically, when the subroutine returns, Perl discards all such references automatically, and you're back to four references again.

We can kill off each reference by using the variable for something other than a reference to the value of @skipper. For example, we can assign undef to the variable:

$reference_to_skipper = undef;

Or, maybe we just let the variable go out of scope:

my @skipper = ...;

{ # naked block
...
my $ref = \@skipper;
...
...
} # $ref goes out of scope at this point

In particular, a reference held in a subroutine's private (lexical) variable goes away at the end of the subroutine.

Whether we change the value, or the variable itself goes away, Perl notes it as an appropriate reduction in the number of references to the data.

Perl recycles the memory for the array only when all references (including the name of the array) go away. In this case, Perl only reclaims memory when @skipper and all the references we created to it disappear.

Such memory is available to Perl for other data later in this program invocation, and generally Perl doesn't give it back to the operating system.


Previous
Table of Contents
Next