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.


In reply to Re: How do I restrict a variable to a range of values? by ikegami
in thread 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.