Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^6: Correct idiom for default parameters

by LanX (Saint)
on Apr 28, 2010 at 23:05 UTC ( [id://837411]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Correct idiom for default parameters
in thread Correct idiom for default parameters

well, thats not the result I get:

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

    Arg! You're right. I tried it against my 5.10 version, because it was sitting there at the command prompt:

    sub test{ my($p1,$p2,$p3) = map shift // $_,(1,2,3); print "p1:$p1; p2:$p2; p3:$p3"; };; [0] Perl> test();; p1:1; p2:2; p3:3 [0] Perl> test( 'a', 'b', 'c' );; p1:a; p2:b; p3:c [0] Perl> test( 'a', , 'c' );; p1:a; p2:c; p3:3 [0] Perl> test( 'a', undef, 'c' );; p1:a; p2:2; 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://837411]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-26 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found