[Date Prev][Date Next][Thread Prev][Thread Next][Author Index][Date Index][Thread Index]

Re: (techy) Code specs?

> On Sun, Nov 15, 1998 at 11:26:23AM -0500, Mark-Jason Dominus wrote:
> > > Some but not all of the functions use prototypes. 
> > 
> > I honestly do not understand what the prototypes are there for anyway.
> > 
> > I imagine that someone put them there because they thought that they
> > were going to get better argument type checking, because that is why
> > other languages have prototypes.  But that is not what Perl prototypes
> > are there for.  Perl prototypes do not provide better argmuent type
> > checking.  Instead, they are there so that you can emulate the
> > behavior of the Perl internal functions.
> Actually, Perl does provide argument checking when you use the -w option.

Not really, because they probably do not have the semantics that you
expect.  For example:

	sub f($) {
	  print $_[0];

	@args = (7,8,9);


I invite you to predict what happens here before you run it.

> Also, the prototypes help document the expected number of arguments.

That is what comments are for.  If you use prototypes for that, you
are using a screwdriver as a chisel.  The manual page says what the
prototypes are for:

       ...the intent is primarily to let you define
       subroutines that work like builtin commands...

The name `prototypes' is unfortunate, because it suggests that the
feature is similar to the `prototype' feature of (say) ANSI C, when it
actually isn't, and leads people to try to use them for argument
checking when actually they are ill-suited for that task. 

I'm not the only person who thinks this; I think most Perl experts
would agree.  For example, here is the last sentence in an article by
Nathan Torkington, co-author (with Tom Christiansen) of _The Perl

        You should probably only use them to mimic the behavior of
        built-in functions like push() and splice().

The article `CryptoContext' was published in _The Perl Journal_ issue #9.

> Well, I'm willing to live without them if everyone else hates them,

I'll do them if you want them, because I don't think it is worth
argiong about, and if you look at my patches you'll see that I did
include prototypes on the functions I introduced.

I am only trying to warn you that

	1) You are not using them for their intended purpose, and
	2) Such usage often leads to subtle and bizarre bugs.

What you choose to do with that information is up to you.