in reply to accessor abuse..?
Using a single method as both accessor and mutator isn't always bad, however this particular usage is because it probably isn't a documented behavior, and because the caller is mucking with "private" data in a way that violates the whole purpose of getters and setters (e.g., encapsulation)
If you want to use a single method for both purposes, it's better to do something like:
sub foo { my ($this, $arg) = @_; $this->{foo} = $arg if defined $arg; return $this->{foo}; }
As to the last point, accessors do serve to protect values, but in Perl unless you do some extra work you're really just depending upon the politeness of your caller to respect your boundaries. For serious encapsulation take a look at closures (Why are closures cool?, Why are closures cool, continued?).
Update: as per tantarbobus's correction.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: accessor abuse..?
by tantarbobus (Hermit) on Aug 22, 2002 at 15:06 UTC | |
by djantzen (Priest) on Aug 22, 2002 at 15:45 UTC | |
by tantarbobus (Hermit) on Aug 22, 2002 at 17:17 UTC | |
|
Re^2: accessor abuse..?
by Aristotle (Chancellor) on Aug 22, 2002 at 23:23 UTC |