How to prompt for input and exit if the user entered an empty string?

前端 未结 2 776
耶瑟儿~
耶瑟儿~ 2021-01-11 12:02

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.

This is

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-11 12:41

    You're almost there.

    print "Enter a word to look up: ";
    my $userword = ; # 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.
    

提交回复
热议问题