This subtle example of autovivification deserves a bit of discussion. It is not something that is self-evident in the docs.

First: autovivification occurs when an lvalue (cling to this word in the paragraphs below) contains references that did not previously exist. So this:

my $href; $href->{smith}{john}{eyecolor} = 'blue';
works because the reference keys are autovivified to provide a slot for the value 'blue'. So far, so good...

But interestingly, even though it does not appear to be an lvalue, the following also causes keys to magically appear:

my $href; my $color = $href->{smith}{john}{eyecolor};
In the process of looking for a value to return, Perl creates (autovivifies) the 'smith' and 'john', keys and then returns undef (eyecolor) to be assigned to $color. The Camel book (3rd Edition, p. 710) describes this while suggesting that this behavior may be "fixed in a future release".

The same goes for:

my $href; if (exists $href->{smith}{john}{eyecolor}) { # do something }

lvalue? bug? You be the judge.

So... to get closer to your code, if we said:

my $aref = undef; my $a = $$aref[0];
We would not thereby create a zero-th element but we would cause the undef $aref to autovivify as a reference to an anonymous array in the process of looking for the non-existent zero-th element.

If you have made it this far, it's only one more step into the fog to suggest that (just as with the previous example):

my $aref = undef; my @a = map {$_} @$aref;
is going to look for a possible $$aref[0] ...and in the process autovivify the $aref as an actual reference to an anonymous array (containing no elements).

Ain't Perl great?
HTH


In reply to Re: side effects of map by dvergin
in thread side effects of map by gregorovius

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.