|Pathologically Eclectic Rubbish Lister|
Re^7: How to return values from a hash?by Athanasius (Abbot)
|on Mar 31, 2013 at 07:16 UTC||Need Help??|
By looking at that most perl programmers would know that it takes 3 mandatory vars, and an optional 4th;
You could also use a runtime check. Compare the subroutines foo and bar:
Both approaches are self-documenting. The advantage of using prototypes is that violations are detected at compile time, which is more efficient. The disadvantage is that the calling syntax is much more restrictive. For example:
works as expected, but
results in a compilation error because the first $ prototype puts @args into scalar context, resulting in a single value of 4 (the number of elements in the array). To call bar successfully in this situation you would need something ugly like this:
Of course, prototypes can be useful; but when you write:
I always use prototypes -- even on methods where they don't matter -- for no other reason than, minimally, documentation. It's a self-imposed discipline.
this sounds (to me) like an attempt to convert Perl into a statically typed language. Much better to embrace Perl’s dynamic nature, use natural Perl idioms by default, and save prototypes for those (rare) situations in which they’re really useful.