Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

prompting a user for input

by Anonymous Monk
on Nov 04, 2001 at 14:01 UTC ( [id://123161]=perlquestion: print w/replies, xml ) Need Help??

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

What is the idiomatic way to prompt for y|yes or n|no as well as including a default answer?

Example:
print "Delete this file?[n]: "; chomp(my $answer=<STDIN>); if ($answer=~/^y|yes$/i){ unlink $file or die $!; } elsif ($answer=~/^n|no|$/i){ next; } else { .....
While writing this I started to think that this might just be a matter of preference so just in case I'll rephrase the question to what are some examples of better ways to go about doing this task.
prompt? accept: y,n,yes,no default: to one of the above continue with program flow
I'm trying to get away from using /^y|yes$/i and making everything else no or n

Replies are listed 'Best First'.
Re: prompting a user for input
by Anarion (Hermit) on Nov 04, 2001 at 15:11 UTC
    I dont know what is the best way, just an if and else, or if and next, i usually use "do that and next if that" but for what you want yo do your regex is wrong, it should be:
    ~/^(y|yes)$/i
    Yours match every answer that begins with y or ends with yes, try for example young or eyes.

    $anarion=\$anarion;

    s==q^QBY_^=,$_^=$[x7,print

Re: prompting a user for input
by joealba (Hermit) on Nov 04, 2001 at 18:00 UTC
    Personally, I like to do something like this:
    if ($answer =~ /^y/i) { unlink $file or die $!; } else { next; }
    So, the user can type "Y", "yessir", "yep", or anything that starts with y. If it doesn't start with y, they mean no.

    If you want something more exact, then Anarion gave you the perfect regexp above.
Re: prompting a user for input
by perigeeV (Hermit) on Nov 04, 2001 at 17:50 UTC

    Here's some fun; reverse the string and the regex in the statement, eg.:

    if ( "" =~ /^\Q$answer/i ) { default() } elsif( "YES" =~ /^\Q$answer/i ) { affirmative() } elsif( "NO" =~ /^\Q$answer/i ) { negative() }

    Now the user can type "y", "ye" or "yes".

    Or if you want you can build a case statement:

    SWITCH: { $answer =~ /^yes|y$/ && do { affirmative(); last SWITCH; }; $answer =~ /^no|n$/ && do { negative(); last SWITCH; }; default(); }

Re: prompting a user for input
by staeryatz (Monk) on Nov 04, 2001 at 23:42 UTC
    Here's a neat regex I've learned for Y/N:
    /(Y(es)?|N(o)?)/i
    this accepts Y, N, Yes, No, and ignores cases.

      You probably want to anchor that at the ends, otherwise 'abcNOdef' and 'blahYblah' will match. Also, the parens around the single char 'o' are unnecessary.
      /^(Y(es)?|No?)$/i

      -Blake

        Looking at:
        ~/^(y|yes)$/i
        This would be my choice:
        ~/^(?:y(?:|es))$/i;
        matches y or yes without any regard to case and doesn't store the match in $1 unnecessarily. TMTOWTDI

        -tengo mas que aprender

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://123161]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-24 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found