I am putting a sub from a program that I wrote called Program Archiver.

This is a generic question answering subroutine

Here is the sub and some examples.

#Useage #Plain yes/no if (stdin_question() =~ /yes/) { #Do something } #Y or N if (stdin_question('y,n','(Y)es (N)o') =~ /y/) { #Do something } #On or off if (stdin_question("on,off","(on or off) :",1) =~ /on/) { #Do somethin +g } #Advanced useage $ret = stdin_question("subdir,manual,none","(subdir, manual, none) :") +; #Test return value and do somthing #--------------------------------------------------------------------- +------------- # STDIN_QUESTION # # If no arguments assume yes and no answers. returns a y or n # # $options Holds the list of valid options. # $text Hold the text to be displayed. # $full_match Tests for full match # #--------------------------------------------------------------------- +------------- sub stdin_question { #Get arguments my ($options,$text,$match) = @_; #Get passed in var #Varables my $input; my $flag = 0; my @valid_options; #Check for defintion of options if (!defined($options)) { $options = "yes,no"; } #Check for defintion of text if (!defined($text)) { $text = "(yes or no) :"; } #Check for defination of match if (!defined($match)) { $match = 0; } #Put options in an arry @valid_options = split/,/, $options; #While no match loop while ($flag ne 1) { #Print out std text print $text; #Read and chomp stdin $input = <stdin>; chomp $input; #Loop through arry for match foreach (@valid_options) { #Test forback match if (/$input/i) { #Test for full match to compare on length if ($match eq 1) { if (length($_) eq length($input)) { $flag = 1; } } else { $flag = 1; } } } } #Return answer return lc $input; }

In reply to Re: Yes No Validation by Three
in thread Yes No Validation by J9

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.