I can't explain why some built-ins don't autovivify the hash, even though it needs to be flattened.

It's not about hash flattening, but the fact that arguments are passed into subroutines as references. So saying f( %{ $h } ); for an undefined $h looks up keys and values, which causes the container to spring into existence as a hash reference and be assigned to $h. That is necessary since the arguments to subs are potential candidates for assignment (lvalues)

perl -MO=Concise -le 'my $h;sub f {} f( %{ $h } );' a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 3 <0> padsv[$h:1,4] vM/LVINTRO ->4 4 <;> nextstate(main 4 -e:1) v ->5 9 <1> entersub[t4] vKS/TARG,1 ->a - <1> ex-list K ->9 5 <0> pushmark s ->6 7 <1> rv2hv[t3] lKM/1 ->8 - <@> scope sK ->7 - <0> ex-nextstate v ->6 6 <0> padsv[$h:1,4] sM/DREFHV ->7 - <1> ex-rv2cv sK/1 ->- 8 <#> gv[*f] s ->9

(note the line padsv[$h:1,4] sM/DREFHV with the M flag), which is not the case with print.

perl -MO=Concise -le 'my $h;print( %{ $h } )' 9 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 3 <0> padsv[$h:1,3] vM/LVINTRO ->4 4 <;> nextstate(main 3 -e:1) v ->5 8 <@> print vK ->9 5 <0> pushmark s ->6 7 <1> rv2hv[t2] lK/1 ->8 - <@> scope sK ->7 - <0> ex-nextstate v ->6 6 <0> padsv[$h:1,3] s ->7

Since print doesn't comprise modifcation of it's arguments, the hashref isn't created.

That's the most concise explanation of this oddity I can conceive up to now...

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re^2: Keys() required to autovivify? by shmem
in thread Keys() required to autovivify? by jrw

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.