develooper Front page | perl.perl5.porters | Postings from October 2012

sub signatures - too many arguments

Thread Next
From:
Peter Martini
Date:
October 22, 2012 19:37
Subject:
sub signatures - too many arguments
Message ID:
CAFyW6MQWQCScDjsf-Ac-5zPuAO9XZZt_GNr2ftpB+tpAZ23xqA@mail.gmail.com
A separate thread for an important question - what should the following do:
sub foo($bar,$baz) {}
foo(1,2,3);

For the case with too many arguments and a non-greedy final (the above), I
think the two real options are:

1. Make it die.  I understand there's a parallel between my ($a,$b) =
(1,2,3), but the primary difference between the two is the 'my' declaration
is strictly under the control of the author, while in the case of a sub,
there's a clear distinction between the author of the sub and the callers.
The ${^ARGS_CNT} gets us at least a way to see if it was called with too
many parameters, but what if the writer of the sub wanted it to die and
complain with what the args are?  I'd rewritten parts of mktables with the
new syntax as a way to find any bugs, and it does this as an integrity
check.

2. Make it warn.  That would shift the burden from the callee to the
caller, which may actually fit better with the language as a whole: as a
writer of the sub, it shouldn't matter to me if they give me something I
can't use, and as the user of the sub, I can turn on the warning or make it
fatal to my hearts content.  The ban on @_ is a guarantee that if the sub
was declared with 3 parameters, it will only use those 3, so using extra
parameters does not actually affect the writer of the sub.

(or 3, ignore it - are there any takers in favor of silently ignoring?)

Option number one gives more control to the writer of the sub, option
number two to the user.  Number 2 seems the most sensible to me, since this
is strictly an error on the part of the caller and can't change the
behavior of the callee.

It's late, and I've rewritten pieces of this several times, so I'm no
longer sure its coherent.  Sending anyway, I hope I've gotten the essential
distinction down.


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About