in reply to autovivified hash key with foreach $x ( @{$h{a}} ) {}

Probably because of the aliasing of $x

Edit

Here another kind of aliasing with the same "weirdness"

$ perl -wE 'my %h; sub tst{ say @_ }; tst ( @{$h{a}} ); exists $h{a} a +nd print "true\n"' true

I seem to remember we already had a similar discussion here not long ago...

Update

Now I remember, because of the aliasing, it's also an lvalue, i.e. it's potentially possible to assign to $x and consequently create the key.

But the autovivification happens at the moment of aliasing, and not at the moment of assignment, because it's too complicated to implement.

And yes, we had this discussion some month ago, I'll try to find the thread.

Bottomline was that it's very complicated to cover all edge cases, and the devs want to keep the code simple.

Update

Compare unexpected modify hash in a distance with grep { $_ }

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: autovivified hash key with foreach $x ( @{$h{a}} ) {}. (Update x2)
by ikegami (Patriarch) on May 18, 2024 at 12:51 UTC

    because of the aliasing, it's also an lvalue

    More precisely, because a modifiable alias is required.

    because it's too complicated to implement.

    I don't think so. We already do that for hash elements, and the same approach could be used to handle more complicated cases. It's a question of performance.

        What about it?