Let's say I have
use v5.026;
use feature 'signatures';
sub foo ($opt1, $opt2) {
say $opt1 if $opt2;
}
main::foo(1,2);
main::foo(1);
Now I want to call foo
with and without opt2:
foo(1); # not currently accepted
foo(1,2); # works fine
Optional parameters with subroutine signatures require a defined default which is done with = default_value_expression
. You can hard set this to undef
:
sub foo ($opt1, $opt2 = undef) {
say $opt1 if $opt2;
}