perl exact string match

前端 未结 3 1147
忘掉有多难
忘掉有多难 2021-01-19 13:30

I have following Perl code to prompt user for yes/no answer. If the user enters anything else than yes/no, keep prompting. No other word is acceptable. I don\'t know why thi

3条回答
  •  情深已故
    2021-01-19 13:51

    Because you're using a regular expression. You could write the regular expression to match the beginning or end of the string ... like this:

    while( $file_yes_no !~ /^(yes|no)$/ ) {
    

    The ^ and $ are the beginning and end of the string. Also you can omit the m.

    Or you could just check the values explicitly:

    while( $file_yes_no ne "yes" and $file_yes_no ne "no" ) {
    

    Also you have a typo in your system command but I'm assuming that was just copying it here. You really shouldn't branch out to a shell for that. Look into File::Copy which gives you a copy function

提交回复
热议问题