如果用户输入空字符串,如何提示输入和退出? [英] How to prompt for input and exit if the user entered an empty string?

查看:38
本文介绍了如果用户输入空字符串,如何提示输入和退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Perl 的新手,我正在编写一个程序,我想强制用户输入一个单词.如果用户输入一个空字符串,那么程序应该退出.

I'm new to Perl and I'm writing a program where I want to force the user to enter a word. If the user enters an empty string then the program should exit.

这是我目前所拥有的:

print "Enter a word to look up: ";

chomp ($usrword = <STDIN>);

推荐答案

大功告成.

print "Enter a word to look up: ";
my $userword = <STDIN>; # I moved chomp to a new line to make it more readable
chomp $userword; # Get rid of newline character at the end
exit 0 if ($userword eq ""); # If empty string, exit.

这篇关于如果用户输入空字符串,如何提示输入和退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆