The only really good use for prototypes is the creation of syntactic sugar. Perl prototypes can let you do neat things like write your own custom grep function that you can call like this:

grepilicious {...} @someArray

Prototypes can also provide limited parameter checking but there are a lot of gotchas involved. In fact, so many that the standard advice is not to use them that way. Here are two:

Protypes can also interfere with good programming practice if you aren't careful. Passing @_ is a good way to cleanly define two almost alike subroutines. It is a lot less prone to typos than extracting all the parameters and then passing them one by one to a subroutine that takes the same parameters:

sub fooExtra { my $somethingExtra=shift; my $result=foo(@_); # combine $somethingExtra and $result in some way # and return, for example .... return $result + $somethingExtra; }

But if you define foo($;$) with a prototype, the above code won't work. Instead of passing the parameters, you'll pass the size of @_, which is not at all what you wanted.

Best, beth


In reply to Re: USAGE OF Prototype by ELISHEVA
in thread USAGE OF Prototype by abubacker

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.