in reply to Correct idiom for default parameters

Here's one way (requires 5.10+). You'll decide for yourself if it is any better:

sub test{ my $p1 = shift // 1; my $p2 = shift // 2; my $p3 = shift // 3; print "p1:$p1; p2:$p2; p3:$p3"; };; test();; p1:1; p2:2; p3:3 test( 'a' );; p1:a; p2:2; p3:3 test( 'a', 'b' );; p1:a; p2:b; p3:3 test( 'a', 'b', 'c' );; p1:a; p2:b; p3:c

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Correct idiom for default parameters
by Anonymous Monk on Apr 29, 2010 at 07:41 UTC

    What about the more obvious following?

    sub test{ my $p1 = shift || 1; [...]

      Suffers because you cannot set $p1 explicitly to 0 (or '' or undef). That's why 5.10 has //.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.