The one place where Prototypes are useful* is if you want to write a replacement for a built-in function that has its arguments parsed in the same way as the builtin. For example, prototype("CORE::lc") is "_", and let's say we want to write a replacement:

use warnings; use strict; use feature 'say'; use Data::Dump 'pp'; sub mylc (_) { print "in=", pp @_; return lc $_[0] } sub mylc2 { # no prototype for comparison print "in=", pp @_; return lc $_[0] }

The prototype does three things:

  1. There is only one argument. If lc or its hypothetical replacement didn't have a prototype, then lc "x", "y" would be the same as lc("x", "y"), but because of the prototype, the former is the equivalent of lc("x"), "y". See also Named Unary Operators.

    say "lc: out=", pp( lc "A","B" ); print "mylc: "; say " out=", pp( mylc "A","B" ); print "mylc2: "; say " out=", pp( mylc2 "A","B" ); # lc: out=("a", "B") # mylc: in="A" out=("a", "B") # mylc2: in=("A", "B") out="a"
  2. Like the '$' prototype, the argument is forced into scalar context. If lc didn't have a prototype, if you said lc(@x), its arguments @_ would be the elements of @x. But because of the prototype, the former is actually equivalent to lc(scalar(@x)), meaning it gets only one argument, the number of elements in @x.

    my @x=qw/ X Y Z /; say "lc: out=", pp( lc @x,"B" ); print "mylc: "; say " out=", pp( mylc @x,"B" ); print "mylc2: "; say " out=", pp( mylc2 @x,"B" ); # lc: out=(3, "B") # mylc: in=3 out=(3, "B") # mylc2: in=("X", "Y", "Z", "B") out="x"
  3. It automagically uses $_ when no argument is passed to the function.

    $_ = 'Z'; say "lc: out=", pp( lc,"B" ); print "mylc: "; say " out=", pp( mylc,"B" ); print "mylc2: "; say " out=", pp( mylc2,"B" ); # lc: out=("z", "B") # mylc: in="Z" out=("z", "B") # Use of uninitialized value $_[0] in lc at example.pl line 12. # mylc2: in=() out=("", "B")

* However:

therefore, they are firmly in the "only use this if you know what you are doing and can explain exactly why" category, or, rephrasing that for non-experts, "Don't use subroutine prototypes"...

Update: Switched code from oneliners to a script.


In reply to Re^3: Too many arguments for subroutine by haukex
in thread Too many arguments for subroutine by Anonymous Monk

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.