[Mpls-pm] Closure Code Samples

Dave Rolsky autarch at urth.org
Thu Oct 13 16:22:40 PDT 2005


On Thu, 13 Oct 2005, Robert Fischer wrote:

> I've read that article, and I'm unconvinced.  Yes, prototypes often do
> things you might not intend them to do (e.g.: length(@arr)) -- on the
> other hand, what would be the alternative?  Assuming length is designed to

What do you mean what's the alternative?  What's the point in the first 
place?  Perl's prototypes do so little that they're basically useless 
except for a few special cases.

> @arr in the previous example.  Furthermore, there's the whole slew of 
> +1/-1/etc./etc. issues that resulted in the creation of prototypes in 
> the first place, and I'm willing to consider those more significant, 
> although Tom dismisses them.

I think if you were to take a survey of code on CPAN _today_ you'd find 
very very few modern Perl coders using prototypes.  Damian argues against 
them in Perl Best Practices too.

> The major issue is that people don't understand what prototypes are, and 
> they are expecting them to do things they don't do, or to not do things 
> they do (like force context).  Like so many things in Perl, you should 
> only use them once you understand them, and you can really hurt yourself 
> if you happen to be using them wrong.  I don't tend to use those 
> patterns that Tom laments (e.g.: passing a two-element array to denote 
> the two arguments of a subroutine), so I find prototypes to be a good 
> way to tip the compiler off and check my own mess.

I understand very well what prototypes are, which is why I don't use them!

> Of course, my personal opinion is that the correct solution is implicit
> static types, like OCaml has:
> http://caml.inria.fr/pub/docs/manual-ocaml/manual003.html
>
> But, then again, I'm particularly biased.

This is certainly more useful than Perl's prototypes.

>> I tried swapping all of your parameter validation for Params::Validate
>> while this is just my opinion, it turned out to be oodles easier to
>> read.
>>
> I never looked into Params::Validate before.  Thanks for the tip.

It can also replace your use of prototypes, since it's a far better way to 
ensure that your arguments are what you expect.  Saying "I expect two 
scalars" doesn't really mean anything AFAIC.


-dave

/*===================================================
VegGuide.Org                        www.BookIRead.com
Your guide to all that's veg.       My book blog
===================================================*/


More information about the Mpls-pm mailing list