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

Don't you to return $this->{foo} and not $this->foo.

Also, foo() breaks if you try to undefine $this->{foo} as in $this->foo(undef); so:

$test->foo("bob"); print "Bob:".$test->foo(); $test->foo(undef); print "Undef:".$test->foo();
Will give:
Bob:bob
Undef:

Instead of checking for defined() maybe look @ the number of args that are passed into foo, like such:
sub foo { my ($this, $arg) = @_; $this->{foo} = $arg if @_>1; return $this->{foo}; }

Replies are listed 'Best First'.
Re: Re: Re: accessor abuse..?
by djantzen (Priest) on Aug 22, 2002 at 15:45 UTC

    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.

      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.