in reply to Re: Yes No Validation
in thread Yes No Validation
That first Regexp will fart. See, the way it's written, it will match either a 'y' at the beginning of the string, a 'yes' or 'n' anywhere in the string, or a 'no' at the end. You need to put parens around the patterns to isolate them from the anchors. Also, I'd prefer an elsif to nested ifs:
if (/^(y|yes)$/i) { print "YIPPEEE.!!!!\n"; } elsif (/^(n|no)$/i) { print "Begone, evil peon!\n" } else { # Validate user input and return message if invalid print "\n\n=====================================\n"; print "You Have Entered and Incorrect Option\n"; print "Valid Options are [Y/N]\n"; print "=====================================\n\n"; &answer; }
However, I would much prefer to use a for as a switch case:
SWITCH: for ($input) { /^(y|yes)$/i && do { print "YIPPEEE.!!!!\n"; last SWITCH; }; /^(n|no)$/i && do { print "Begone, evil peon!\n"; last SWITCH; }; # Validate user input and return message if invalid print "\n\n=====================================\n"; print "You Have Entered an Incorrect Option\n"; print "Valid Options are [Y/N]\n"; print "=====================================\n\n"; &answer; }
|
|---|