in reply to Re^5: Correct idiom for default parameters
in thread Correct idiom for default parameters
sub defaults { my( $p1, $p2, $p3) = map{ defined $_[0] ? shift : $_ }( 1, 2, 3 ); + print "p1:$p1; p2:$p2; p3:$p3\n"; };; defaults('a', undef, 'c');
p1:a; p2:2; p3:3
$p3 ne 'c'
the first undef value omits the shift, all following parameters will be replaced by defaults! You need an explicit shift after the colon!
Cheers Rolf
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Correct idiom for default parameters
by BrowserUk (Patriarch) on Apr 28, 2010 at 23:24 UTC |