Unfortunately, without the wrapper (push $future_arrayref, @values) that will fail with an error. Shouldn't that autovivify $future_arrayref?

I don't think push should autovivify because that would be a surprise (no built-ins autovivify) and it would hide delay discovering errors (pushing onto the wrong variable).

I feel the current documentation for push is wrong, as push doesn't really dereference anything. The new experimental feature merely saves you from manually having to dereferencing to satisfy prototypes -- push always took an array ref as an argument but required you to type  push @array instead of  push \@array and now it drops the requirement so you don't have to write  push @{$array}

Only dereferencing in lvalue context autovivifies (more on this in Re: undefined value as an ARRAY reference sometimes ok ) and plenty of newbies complain about this already :)( see When DOESN'T "Use of uninitialized value" show up? )

So why is this new experimental feature more than mere keystroke saving sugar? Because it doesn't autovivify. Consider this

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump; my @foo; my $bar; silentWin( \@foo ); silentFail( $bar ); dd \@foo; dd $bar; aliasUglyWin( $bar ); dd $bar; print "bye\n"; silentWin( undef ); print "bye\n"; sub silentWin { my( $arrayref ) = @_; push $arrayref, "win"; } sub silentFail { my( $arrayref ) = @_; push @{$arrayref}, "fail"; } sub aliasUglyWin { push @{ $_[0] } , "ugly win"; } __END__ ["win"] undef ["ugly win"] bye Not an ARRAY reference ...

aliasUglyWin() works but its ugly :)  $_[0] is aliased to $bar, so the correct variable ($bar) is autovivified

silentFail() fails because when you copy  $_[0] to $arrayref, $arrayref is not aliased to $bar , so the wrong variable is autovivified ( not $bar)

silentWin() saves you three keystrokes @{} and it doesn't autovivify the wrong variable but dies instead . It also changes the diagnostics message from Type of arg 1 to push must be array ... to Not an ARRAY reference

So I don't think push should autovivify, I feel it defeats what little advantage this new experimental feature provides to the uninitiated


In reply to Re: Should perl auto vivify here? by Anonymous Monk
in thread Should perl auto vivify here? by randian

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.