in reply to Re: Re: accessor abuse..?
in thread accessor abuse..?

Well, depends on how you want to go about signifying a 'null' value. Recall that an empty string is both defined and a common way of indicating that variable holds no useable value. So, calling the method like foo('') will effectively unset the variable. Of course in more complex situations (i.e., where the value to be set is an object reference, not just a string) you'd need something better, probably something along the lines of your suggestion to pass undef as a parameter.

Replies are listed 'Best First'.
Re: Re: Re: Re: accessor abuse..?
by tantarbobus (Hermit) on Aug 22, 2002 at 17:17 UTC

    I agree with you about using an empty string, but if you are going to be ignoring the undef case, then it probably should be documented because if someone, like me, were to wonder upon that bit of code, s/he might be tempted to see the behaviour as a bug and not a feature (after all it did not do what I told it to when I spoke $self->foo(undef) :) So maybe:

    ########## # foo() # Sets or returns the value of foo. # But will keep the old value of foo # if undef be passed in sub foo{ # Do foo stuff }

    Another option would be to use a different function name that reflects the no-blow-away-existing-values-on-undef behaviour.