Modules can have function prototypes. They just aren't checked when a function is used as a method. For that matter, prototypes in Perl aren't checked when you call the function with a leading '&' either. Basically, prototypes in Perl aren't at all like prototypes in other languages and they'll bite you if you expect them to be.
package Class;
sub new { bless {} }
sub method ($$) {my $self=shift; my $arg=shift }
package main;
my $object = Class->new();
$object->method(); # OK
$object->method("foo"); # OK
Class::method($object); # fails. Whines about arg count.
&Class::method($object); # OK
Given this, if you have a routine that might do something particularly nasty when called incorrectly, you should probably just write some specialized checking into that routine. Generalized error checking for all (or even most) subs isn't worth the effort.
In Perl, the best way to ensure your client (meaning the person using your module) uses it correctly is to thoroughly document your interface. Trying to protect your module against all the mistakes that someone might make is laudable but it's probably not possible. Spending your time writing better documentation will pay off a lot more in the long run than spending it trying to code bulletproof error checking.
Perl's prototypes are useful if you need to have a function that acts (mostly) like a builtin but otherwise I suggest abstaining. You are probably better off not using them at all until you really understand what they are and how they differ from prototypes in other languages.
-sauoq
"My two cents aren't worth a dime.";
|