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.";

In reply to Re: Preffered Error notification? by sauoq
in thread Preferred Error notification? by RollyGuy

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.