http://qs321.pair.com?node_id=486272


in reply to Re: Things I Don't Use in Perl
in thread Things I Don't Use in Perl

As implemented, your example would fail if the called in scalar context, as in $name = $object->name;, because wantarray will return false if you've called in scalar context. To check for void context, you need to check if the result of wantarray is undefined. This illustrates how easy it is to make a mistake with single get/set methods. Also, what if it is valid to set the name property to an empty string, the string '0' or undef? How would you do that with this combined get/set? Oh, you could play with wantarray to find out if you're getting, but what if (as is the case in the original, and common) the set method is to return the value set? Wantarray will be defined, so you have no way of reliably knowing if you were really called as get or set.

It is more readable, safer, and simpler to code:

sub set_name { my $self = shift; ## validation /untainting here ## $self->{name} = shift; return $self->{name}; } sub name { my $self = shift; if (@_) { warn "Attempt to set value in get" } return $self->{name}; }
<-radiant.matrix->
Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law