Operating System - HP-UX
1832617 Members
3545 Online
110043 Solutions
New Discussion

Re: Built-in function to accept "yes" "no" response?

 
Mike Keys
Regular Advisor

Built-in function to accept "yes" "no" response?

I can't search the forums at the moment, but wanted to know if perl had any nifty way of only accepting a yes (y) or no (n) response from standard input, instead of having to build in the logic to handle these keys.

My if statements are growing because of all the y/n logic that needs to be done.
6 REPLIES 6
Mel Burslan
Honored Contributor

Re: Built-in function to accept "yes" "no" response?

I do not think there is one built in but I have this one grafted from a web page and saved at one time:

sub confirm()
{
$UserAnswer = <>; # Read in the user's answer.
chomp $UserAnswer;
if ((not($UserAnswer eq "n")) and # If it's not a yes or a no,
(not($UserAnswer eq "y")) and
(not($UserAnswer eq "N")) and
(not($UserAnswer eq "Y")))
{
while ((not($UserAnswer eq "n")) and # Keep going until we get a yes
(not($UserAnswer eq "y")) and # or a no.
(not($UserAnswer eq "N")) and
(not($UserAnswer eq "Y")))
{
print "Please answer \"y\", for yes, or \"n\", for no.\n";
$UserAnswer = <>;
chomp $UserAnswer;
}
}
# If we've gotten here, the user has given a yes or a no answer.
return (($UserAnswer eq "Y") or ($UserAnswer eq "y"));
}

then using it

print "Do you wish to continue (y/n)? ";
if (&confirm())
{
print "Continuing...\n";
}
else
{
print "Bye!\n";
exit 0;
}


I saved it for future use in my useful stuff folder but never tested to see if it works. Hence, proceed with caution ...
________________________________
UNIX because I majored in cryptology...
Mike Keys
Regular Advisor

Re: Built-in function to accept "yes" "no" response?

This is pretty much what I am doing with the if statements. I was wondering if there was a function of sub routine that would accept only three inputs: y/n or ctrl-c.

Basically, what I wanted was a way to prevent any other input, when asking for a response without having to build in the extra "else" statement to handle all other keystrokes.

If there isn't an easy way, then I'll resort to the extra 'else'.
Patrick Wallek
Honored Contributor

Re: Built-in function to accept "yes" "no" response?

I'm not a PERL expert by any means, but does PERL have a case statement equivalent?

In a shell script you could do something like:

case ANSWER in
Y|y|yes|YES|Yes|YEs|yES|YEs|yeS) do something ;;
N|n|NO|No|nO) do something else ;;
*) error condition for all other input ;;
esac


Just a thought!
David Child_1
Honored Contributor

Re: Built-in function to accept "yes" "no" response?

You might be able to use perl's 'switch' (http://perl.active-venture.com/lib/Switch.html)

Another shell option that is similar is;

$ cat ddd.sh
#!/usr/bin/ksh
echo "Do you know what your doing?"
select ENTRY in yes no
do
echo "You selected: $ENTRY"
break
done

$ ./ddd.sh
Do you know what your doing?
1) yes
2) no
#? 1
You selected: yes

A. Clay Stephenson
Acclaimed Contributor

Re: Built-in function to accept "yes" "no" response?

Perl really does not have a switch or case statement.

You write subroutines to handle basic tasks like this:

my $resp = ask_yn("Are you a whackball?");
ask_yn() returns 1 if yes/y/YeS/Y/ ... is pressed; 0 if no/NO/n/nO/N ... is pressed; and the function returns -1 if SIGINT is received. It loops until a valid response is typed (or a SIGINT is received).

It's really time for you to go out and buy the O'Reilly Perl (Camel) books. The good news is that once you start writing Perl, you really don't need shell, awk, sed, grep, et al.

The attached ask_yn.pl should make a good subroutine for you. Note: "die" inside an eval does not exit the program just exits the enclosing eval {} and that's a sneaky way to implement signal handlers in Perl.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Built-in function to accept "yes" "no" response?

By the way, at the risk of confusing you (I intentionally watered down the subroutine), you can actually make the subroutine a bit more lenient so that leading and trailing whitespace doesn't clobber your yes/no responses. i.e ' Yes ', 'n ', or ' Y ' will also work by making the following changes:

if ($q =~ /^(yes|no|y|n)$/)
becomes
if ($q =~ /^\s*(yes|no|y|n)\s*$/)

\s* means zero or more whitespace characters

$rslt = (substr($q,0,1) eq "y");
becomes
$rslt = (substr($1,0,1) eq "y");


Regular expressions inside ()'s become $1, $2, ... for each set of parens.

The change is up to you but it does make things easier for the user at the slight expense of greater Perl novice confusion.
If it ain't broke, I can fix that.