Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Re: Default subroutine parameters (boo)

by thelenm (Vicar)
on Apr 23, 2002 at 16:21 UTC ( #161356=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Default subroutine parameters (boo)
in thread Default subroutine parameters

I really like this solution because it's syntactically simple. It works when an argument is omitted entirely, but unfortunately it doesn't supply the default value when the value 'undef' is passed as an argument, like this:
mysub(foo => 0, bar => 1, baz => undef); sub mysub { my %defaults = qw(foo 9 bar 8 baz 7); my %args = (%defaults, @_); print "Foo: $args{foo}, Bar: $args{bar}, Baz: $args{baz}\n"; }

But it's inspired me to see if I can modify it to suit my needs. Thank you!

Replies are listed 'Best First'.
Re: Re: Re: Re: Default subroutine parameters (boo)
by Fletch (Chancellor) on Apr 23, 2002 at 16:24 UTC

    It doesn't supply a default because you've supplied a value (undef). If you want the default, don't supply something with that key as a parameter. You're presuming an idiom `replace undef parameters with the default value' where this code implements `replace unspecified parameters with the default value'.

      You're presuming an idiom `replace undef parameters with the default value'

      Right, that was the question I asked. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://161356]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2016-12-09 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (158 votes). Check out past polls.