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

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 what I have so far:

print "Enter a word to look up: ";

chomp ($usrword = <STDIN>);

Answers


You're almost there.

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.

File output is buffered by default. Since the prompt is so short, it is still sitting in the output buffer. You can disable buffering on STDOUT by adding this line of code before printing...

select((select(STDOUT), $|=1)[0]);

Need Your Help

"No source for code" message in Coverage.py

python continuous-integration code-coverage nosetests

I ran a build last night, successfully. I got up this morning and ran another without changing any configuration or modifying any source code. Now my build is failing with the message "No source fo...

(= 1/5 0.2) is false?

lisp scheme common-lisp

This is in both common lisp (clisp and sbcl) and scheme (guile). While these are true: