O monks, how do I restrict a variable to a range of values?

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
use 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
I realize I could use fields qw(good bad unknown)and do something like
$status->{good} = '1'; #should be okay $status->{bad} = '1'; #should be okay $a_or_b->{ungnown} = '1'; #should generate an error
However, this doesn't satisfy me because the status should not be allowed to be simultaneously good and bad.

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}; }

In reply to How do I restrict a variable to a range of values? by tphyahoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.