gsus has asked for the
wisdom of the Perl Monks concerning the following question:

Evening Perl Monks

I have a really simple requirement on a project that i'm working on at home which requires what i believe is a while loop. In it's most simple form, i want to prompt a user running a script to enter either Y or N to continue or abort the script.

Below is what i thought might work, oh and i have this in a subroutine.

If the input were 'y', the first part of the || would be false, but the second part would be true. For an input of 'n', the first part is true while the second is false. Any other input, both parts are true. The only way to avoid the loop if the input is simultaeneously 'y' and 'n'. Only Damian can do that.

Below, ask_yes_no("prompt XXX") keeps prompting with "prompt XXX" until either a 'Y' or a 'N' answer happens or a quit/abort input is seen which causes the program to completely stop.

A non-answer (blank line) causes just a re-prompt (that's not an error as per standard CLI).

An invalid answer causes an error message and then the re-prompt.

Of course it would be possible to tighten the while loop up so much that it only allows 'Y' or 'N' for the $answer.

The basic idea when prompting the user for an answer:
1) ask the question
2) a "non answer" like a bunch of blanks is ok, just re-prompt
3) an "illegal answer".. something that is clearly wrong should
result in an error message and then the re-prompt
4) spaces before or after the "answer" should be meaningless.

In the code below, there are a number of lines of "boiler plate" stuff along the way to returning a Y or a N. It is not necessary to reduce this to the minimum number of lines of code. The computer is ridiculously fast compared with the human being. And sometimes fewer lines of source code does not mean a faster program.

I would advocate something like the below. Ask the question and if the subroutine returns, it has a Y or a N. If it cannot come back with one of those 2 answers, then it should not "come back" to the main program.