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

sub signatures - too few arguments

Thread Next
Peter Martini
October 27, 2012 15:35
sub signatures - too few arguments
Message ID:
Discussions had started on this anyway, but I think its worth discussing

Here's an example to think about:

sub foo {
   my ($bar, $baz) = ($_[0], $#_ > 0 ? $_[1] : 5);

Without $#_ exposed, there's no difference between foo(1,undef) and foo(1),
so its clear the callee has a good reason to want strictness.

Yet, forcing the caller to say foo(1,5) or foo(1,undef) defeats the purpose
of the default - unless undef is a magical value that means use the
default, which puts us right back to the defaults of not strict anyway.

Or, of course, make all optional parameters part of an array, but it's ugly
to have to do:

sub foo($bar, @optional){
   my $baz =$#optional >= 0 ? $optional[0] : 5;
   my $quz =$#optional >= 1 ? $optional[1] : 6;

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About