Your second example with an array isn't really autovivification. Perl hashes are sparse (elements aren't stored contiguously in memory), whereas Perl arrays aren't sparse (the sequence of elements is stored contiguously in memory). When you do my %h; $h{ 100 } = 2; the one SV* is stored; when you do your example, Perl has to allocate room to store 101 SV*s and store the SV* for the 2 in the 101st (the other 100 contain pointers to sv_undef).

Autovivification is when Perl allocates a reference to a new anonymous hash or anonymous array on the fly; this is just how arrays work. Now if you did $a[100][42] = 3.14159 that would be autovivification in that it would create a new arrayref (again, with room for 43 elements preallocated) and store it into $a[100] and then store 3.14159 into the last element.

The difference from C or Java is that Perl will automatically extend arrays for you (which again isn't autovivification), whereas the other languages you do have to manually allocate or extend them.


In reply to Re^2: Should this happen? by Fletch
in thread Should calling 'exists' create a hash key? by mje

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.