To be honest, the only places I rely on autovivication are: hash values, array values, and anywhere explicitly documented to use autovivication (e.g., IPC::Open3), although in that last case, I got bit where it promised autovivication and didn't deliver, so I manually "vivified" the handles before passing them in.

Top-level references, for some reason, I just never tried to use autovivication. I'm not sure why.

Anyway, we already know that builtins are treated special in many ways - this just seems to be another one. I'm sure we could do something similar with XS somehow .. but I'm not an XS expert. I think perl6 solves this difference making builtins behave the same way as user-defined functions, or, more to the point, allowing user-defined functions to gain builtin-like semantics.

In your case, the user-defined function autovivifies the array to figure out what @_ will contain. Why perl doesn't need to do likewise for builtins, I'm not sure. Or why perl doesn't figure out that $x being undef will autovivify to an empty list and just skip the autovivication, I'm not sure.

Note that builtins that treat the array as an lvalue (like using '@' in a prototype) do autovivify:

$ perl -Mstrict -le 'my $x; push(@$x,"z"); print "=> ", ref $x, $/' => ARRAY
so it's not all builtins. But it does seem to be all user-defined functions - or at least I've not found a counter example.


In reply to Re: Autovivification of scalars in sub calls by Tanktalus
in thread Autovivification of scalars in sub calls by tlm

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.