Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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
Replies are listed 'Best First'.
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: (13)
As of 2015-07-31 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls