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

i would like to ask about something that Randal Schwartz( merlyn ) says in his book(Alpaca). he says

if we copy Gilligan's provisions:
my $gilligan_stuff = $all_with_names[2][1];
then when we remove @all_with_names, we still have one live reference to what was formerly @gilligan, and the data from there downward remain alive.

i would like to ask reffering to the meaning of "downward". does it mean that all items with indexes like [i][j] with i=>2&&j>1 remain available ?

thank you

Replies are listed 'Best First'.
Re: deleting array references
by clinton (Priest) on Jun 20, 2007 at 09:37 UTC
    No, I don't know what the original data was, but it would mean something like this:

    If we have this variable:

    my $a = [ { foo => { foo2 => { foo3 => 'xxxx' } }, bar => { bar2 => { bar3 => 'yyyy' } } }, { FOO => { FOO2 => { FOO3 => 'xxxx' } }, BAR => { BAR2 => { BAR3 => 'yyyy' } } }, ];

    And we assign variable $b to point to part of it, and then we undef the original:

    my $b = $a->[1]{BAR}; undef $a;

    Then only the references referred to by $b continue to exist:

    use Data::Dumper; print Dumper ($a,$b); > $VAR1 = undef; $VAR2 = { 'BAR2' => { 'BAR3' => 'yyyy' } };

    Clint

      great,thanks
Re: deleting array references
by shmem (Chancellor) on Jun 20, 2007 at 10:06 UTC
    does it mean that all items with indexes like ij with i=>2&&j>1 remain available ?

    No. The array @all_with_name is cleared. The element at position 1 in the array reference in $all_with_names[2] has been copied to $gilligan_stuff, so it is there. If the thingy copied was a plain scalar, $gilligan_stuff holds a brand new copy of the contents of the array slot $all_with_names[2][1].

    But if the entry in that slot was a reference, by copying that to $gilligan_stuff the reference count of the thingy that reference points to was increased, and the reference in $all_with_names[2][1] pointed to the same thingy.

    By destroying the array @all_with_names the original data $all_with_names[2][1] was pointing to has not been deleted, although the slot $all_with_names[2][1] has been destroyed - only the pointer has been deleted. The reference count of the underlying data was decreased by deleting the array which held a reference to it, but until that count goes to zero the data is still allocated and accessible (if you have a refrence :-).

    HTH,

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}