Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Correct idiom for default parameters

by mrider (Sexton)
on Apr 28, 2010 at 22:52 UTC ( #837405=note: print w/ replies, xml ) Need Help??


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

That looks pretty good!

And to answer your question in the post previous to this one, no I don't expect there to be an "undef" in the middle. The overwhelming majority of the cases there is one parameter that is optional. Which means that the predominant use will look like this:

sub defaults { my ($self, $param) = map { @_ ? shift : $_ } (1); print($param); }

I found one place where it would be nice to have two optional parameters, but I'm debating refactoring that sub routine.


Comment on Re^3: Correct idiom for default parameters
Download Code
Re^4: Correct idiom for default parameters
by LanX (Canon) on Apr 28, 2010 at 22:58 UTC
    >  my ($self, $param) = map { @_ ? shift : $_ } (1);

    ehm ... $self defaults to 1??? ;-)

    IMHO better catch $self in a separate shift!

    UPDATE:

    > The overwhelming majority of the cases there is one parameter that is optional. Which means that the predominant use will look like this:

    Then better avoid this construct!

    IMHO it's much clearer (and faster) to write

    my ($self,$a,$b,$c,$d)=@_; $d = 4 unless defined $d;

    Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2014-12-18 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls