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:
- There is no guarantee that parameter checking will even take place. Perl only checks parameter prototypes when a subroutine is called as a function and has no leading &. If user decides to call your subroutine like $x->foo() or MyClass::X->foo() or even &foo($x,...), then Perl will happily ignore your prototype. A subroutine author has no control over how people call his/her code, the subroutine has to be written as if there were no error checking at all.
- Even if parameter checking does take place, a
prototype will only complain about bad parameters
if it can't coerce the arguments you pass into good
parameters. This can lead to strange and hard to
track
down bugs. For example, normally you would expect that
foo(@someArray) would pass one parameter for each
member of the array @someArray. So, for example,
if @someArray=(1,2) then foo(@someArray)
would be the same as passing foo(1,2)
However, if you declare something with a prototype
foo($;$) and then try to pass it
foo(@someArray) or some other array, Perl will
pass the size of @someArray, not the
array elements. This is because the first $ of foo($;$)
forces a scalar context on @someArray and an array in scalar
context evaluates to its size.
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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.