in reply to Re^9: Use of freed value in iteration
in thread Use of freed value in iteration

I'm not sure I'd want that second print "@_\n" to print aaa bbb.
I'd want it to. Consider this:
my @a = qw(aaa bbb); my $r = \$a[0]; @a = (); print $$r;
I'd expect that to print "aaa". This is pretty much at the heart of the reference-counting model at the heart of perl. Similarly to how the fact that the lexical var getting freed on scope exit doesn't free the return value in:
sub f { my $x = ...; return $x }

Dave.

Replies are listed 'Best First'.
Re^11: Use of freed value in iteration
by Danny (Chaplain) on Feb 25, 2024 at 21:58 UTC
    "This is pretty much at the heart of the reference-counting model at the heart of perl."
    I see what you mean. So do you think that when iterating over values, the refcount of all values being iterated should have their refcount incremented before the loop starts?
      So do you think that when iterating over values, the refcount of all values being iterated should have their refcount incremented before the loop starts
      Under PERL_RC_STACK builds (which will eventually become the default), this will happen automatically, as all items will get their reference count incremented when pushed onto the the argument stack (which doesn't happen at the moment). A for loop by default just iterates over a range of items on the stack.

      Dave.