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


In reply to Re: autovivified hash key with foreach $x ( @{$h{a}} ) {}. (Update x2) by LanX
in thread autovivified hash key with foreach $x ( @{$h{a}} ) {} by Danny

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.