El siguiente ejemplo, debido a Ikegami (PerlMonks), está tomado de una respuesta en PerlMonks. Suponga el siguiente código:
1 my $array_ref = []; 2 for(1 .. 2) { 3 my $hash_ref = { foo => foo, bar => bar }; 4 push(@{$array_ref}, $hash_ref); 5 } 6 7 my $hash_ref = $array_ref->[0]; 8 $array_ref = [];
Estado después de la línea 7:
$array_ref $hash_ref +--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------------+ +--------------+ | o=====> | array | | o | +--------------+ +--------------------+ +---------- : -+ | refcount = 1 | : +--------------------+ : | element 0 | : | +--------------+ | : | | hash ref | | : | +--------------+ | : | | refcount = 1 | | V | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 2 | | element 1 | +--------------+ | +--------------+ | | keys & vals | | | hash ref | | +--------------+ | +--------------+ | | | refcount = 1 | | | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+
Despúes que se sobreescriba la referencia al array (línea 8):
+--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------------+ +--------------+ | o | | array | | o | +---------- : -+ +--------------------+ +---------- : -+ : | refcount = 0 | : : +--------------------+ : V | element 0 | : +--------------+ | +--------------+ | : | array | | | hash ref | | : +--------------+ | +--------------+ | : | refcount = 1 | | | refcount = 1 | | V +--------------+ | +--------------+ | +--------------+ | no elements | | | o========> | hash | +--------------+ | +--------------+ | +--------------+ +--------------------+ | refcount = 2 | | element 1 | +--------------+ | +--------------+ | | keys & vals | | | hash ref | | +--------------+ | +--------------+ | | | refcount = 1 | | | +--------------+ | +--------------+ | | o========> | hash | | +--------------+ | +--------------+ +--------------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+
El array original es liberado automáticamente puesto que su contador de referencias alcanza cero. Las estructuras de los contadores de los elementos alcanzan cero:
+--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ +--------------+ : | array | | hash ref | : +--------------+ +--------------+ : | refcount = 1 | | refcount = 0 | V +--------------+ +--------------+ +--------------+ | no elements | | o========> | hash | +--------------+ +--------------+ +--------------+ | refcount = 2 | +--------------+ +--------------+ | keys & vals | | hash ref | +--------------+ +--------------+ | refcount = 0 | +--------------+ +--------------+ | o========> | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+
Los contadores de referencias de los hashes alcanzan cero y son liberados. Ahora el contador del antiguo segundo elemento alcanza cero:
+--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ : | array | : +--------------+ : | refcount = 1 | V +--------------+ +--------------+ | no elements | | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+ +--------------+ | hash | +--------------+ | refcount = 0 | +--------------+ | keys & vals | +--------------+
y es liberado:
+--------------+ +--------------+ | array ref | | hash ref | +--------------+ +--------------+ | refcount = 1 | | refcount = 1 | +--------------+ +--------------+ | o | | o | +---------- : -+ +---------- : -+ : : : : V : +--------------+ : | array | : +--------------+ : | refcount = 1 | V +--------------+ +--------------+ | no elements | | hash | +--------------+ +--------------+ | refcount = 1 | +--------------+ | keys & vals | +--------------+
Casiano Rodríguez León