in reply to How do I restrict a variable to a range of values?

There are many ways. Here are three.

1) Use accessors. This is a great solution is the var is an object.

sub STATUS_GOOD () { 0 } sub STATUS_BAD () { 1 } sub STATUS_UNKNOWN () { 2 } sub set_status { my ($self, $status) = @_; $self->{status} = STATUS_GOOD if $status eq 'good'; $self->{status} = STATUS_BAD if $status eq 'bad'; $self->{status} = STATUS_UNKOWN if $status eq 'unknown'; require Carp; Carp::croak("Unknown status"); } sub is_good { return shift->{status} == STATUS_GOOD; } sub is_bad { return shift->{status} == STATUS_BAD; } sub is_unknown { return shift->{status} == STATUS_UNKNOWN; }

2) Use tied variables. They can validate the data being stored into them.

3) You could use constants instead of strings. Misspellings would result in compilation errors. It's still possible for bad data to be given, but they'd have to do it on purpose.