in reply to Quicker way to do this IF statement 1-5
Since an input validation is not likely to be a big user of system resources, an "optimal" solution is one that's easy to read and easy to change.
If the value is being used for something which could be character-based, such as a menu selection, use a hash, as suggested by demerphq. If it's going to be used for something that will be strictly numeric, I would just do something like this:
And a grammatical quibble: "either" should only be used when there are exactly two choices.use strict; use warnings; my $entered_value; my $min_allowed = 1; my $max_allowed = 5; if($entered_value =~ /^\d+\z/s and ($entered_value >= $min_allowed and $entered_value <= $max_allowed) +) { do_magical_processing($entered_value); } else{ print "value must be a number no less than $min_value and not grea +ter than $max_value\n"; }
emc
Insisting on perfect safety is for people who don't have the balls to live in the real world.
|
|---|