Well, as long as you don't change the value it would work the same with shift. Since you've had exemple already, and I'm not sure of what's troubling you, I'll try to answer a question you didn't ask: why? (or when?). In the best case, it'll help you, in a less optimal case, it'll help others, in the worst case scenario, someone will be able to correct one of my misconceptions :D

In most case, the prototype _ as a simple prototype-less equivalent:

sub prototyped($_;$) { # Here $_[0] is a compulsory argument # $_[1] defaults to $_ # $_[2] may not even exist } sub unprototyped { push @_, $_ if @_ < 2; # Here $_[0] is a compulsory argument # $_[1] defaults to $_ # $_[2] may not even exist }
Here I used the prototype $, but _ of course works after \@, \%, + ... (but you can't have an unprototyped equivalent without using source filters)

Now, that's probably enough in most cases, but this might cause some trouble if the function should modify its parameters (like chomp), or if $_ is a tied scalar (you'll call FETCH, which might have unexpected side effects) you can't just push a copy into @_. In this case, the best equivalent code I could come up with (using only core perl) is:

sub unprototyped { goto &unprototyped @_, $_ unless @_ > 1; # See comments in previous exemples }
Now that's where the _ becomes really useful, because not using goto will run faster, and will prevent people who can't tell the difference between the C-style and the other goto from calling you the devil.

NB: for the '$_ is tied or should be modified' problem, an alias would work (but it is not core perl) but not this:

sub unprototyped { push @_, $_ if @_ < 2; # Do all the work $_ = $_[-1]; # Return value here }
Because in '# Do all the work', the modifications and copies of $_ won't trigger STORE and FETCH. A reference to either the parameter or $_ would work though, but it wouldn't be as transparent.

And there is another case where push @_, $_ is not equivalent to the _ prototype, I'll leave this bit of code to show how _ can go wrong:

use v5.14; sub f2 { while (<DATA>) { chomp; last; }; } sub f1(_) { f2; say shift; } f1 "Hello "; $_ = "World"; f1; f1 for "!"; __DATA__ Everyone Perlmonks

Edit: of course, in all my exemples the user plays nice and never calls unprototyped without arguments.


In reply to Re^3: Prototype (_)? by Eily
in thread Prototype (_)? by BrowserUk

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.