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

sub signatures - too few arguments

Thread Next
From:
Peter Martini
Date:
October 27, 2012 15:35
Subject:
sub signatures - too few arguments
Message ID:
CAFyW6MQTQO3RQ9=QWyawk_xgmqdyc06huYSX-de69zK4MjR8Og@mail.gmail.com
Discussions had started on this anyway, but I think its worth discussing
separately.

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


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