tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:
I would like to be able to do something like
$self->status('unknown') or $self->status('good') set an object's stat +us, whereas $self->status('ungnown') generates an error. I would like + this also to be available
I realize I could use fields qw(good bad unknown)and do something likeuse strict; use warnings; my $a_or_b; $a_or_b->{a} = 'a'; #should be okay $a_or_b->{b} = 'b'; #should be okay $a_or_b->{c} = 'c'; #should generate an error
However, this doesn't satisfy me because the status should not be allowed to be simultaneously good and bad.$status->{good} = '1'; #should be okay $status->{bad} = '1'; #should be okay $a_or_b->{ungnown} = '1'; #should generate an error
I feel like this could be useful in a range of situations where I want to "program defensively against typos and the like. I have recently started using fields and hash::lock_keys to die when I make an $object->{misspelled} type mistake. This worked so well that my instinct is now to try this on individual variables. Is there a way to do this?
UPDATE: I guess I can do this with something along the lines of $self->set_status('ungnown') and validate against typos with grep like kwaping said. I just thought there might be some sweeter way to do this. But, the answer appears to be, there really isn't, other than *use accessors". So, I guess I will be using accessors...
UPDATE 2: Ha, I knew there was a way -- thanks jediwizard. Jedi presented a way with tied hashes, but what I really wanted was tied scalars, so here's what I'm using:
use strict; use warnings; my($status); tie $status, 'Status'; $status = 'ungnown'; # warning, and value does not get set. $status = 'good'; # works print "$status"; package Status; use Tie::Scalar; use Carp; use base qw(Tie::StdScalar); sub TIESCALAR { my $class = shift; my $status = shift; bless \$status, $class; } sub STORE { my($me, $value) = @_; unless ($value eq 'good'){ warn("invalid status: $value. Status not set"); return undef; } ${$me} = $value; } sub FETCH { my($status) = @_; return ${$status}; }
|
|---|