sub push (\@;@) { CORE::push ( @{+shift}, @_ ? @_ : $_ ) }
already tried it, it's a trap forget it. If you do push (@a,@b) and @b is empty, then $_ will be pushed, and that's definetly not what you expect!

If there is any feature request, which might make sense on the wishlist for the next perl5 version, it's to add a prototype symbol for "list defaulting to $_". "_" is already a symbol for "scalar defaulting to $_" which must be placed as last symbol before ";".

So I suggest double underscore "__" shall mean "list defaulting to $_".

I remember slightly that in PBP one can find a somehow similar reasoning why not to use prototypes! *

If anybody knows a way to already implement push() this way, please try these testcases:

use subs 'push'; sub push (\@;@) { # print $_[0],$_[1]; CORE::push( @{+shift}, $_[0]? @_ :$_) ; } $\="\n"; my @a; $_="_"; print "--- one parameter"; push @a; print @a; print "--- long list of parameters"; @b=("x","y"); @a=(); push @a,@b; print @a; print "--- one element"; @a=(); push @a,"z"; print @a; print "--- one element list"; @a=(); @b=("z"); push @a,@b; print @a; print "--- empty list"; @a=@b=(); push @a,@b; print @a; __END__ --- one parameter _ --- long list of parameters xy --- one element z --- one element list z --- empty list _

Cheers Rolf

UPDATES: Chapter9 "Subroutines"/Subchapter "Prototypes" -> "Don't use prototypes" ... The examples show that they mostly confuse because they often force scalar context!

In reply to Re^7: why does push not default to $_? (simple) by LanX
in thread why does push not default to $_? by LanX

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.