#!/usr/bin/perl print "Please, what is 2+2? You can answer\n"; print " a number to guess\n"; print " ? to ask me again what the question is,\n"; print " R to retry,\n"; print "or Q to quit or A to abort.\n"; # Note that while (<>) { is not quite the same as # the while () { I have below. # # A bare (<>) means to read from the argument list (ARGV) first, # or if it's empty, then and only then, read from STDIN. while () { chomp; $answer=$_; # print "Checking >$answer<\n"; if ($answer) { # Catch null answer if ($answer =~ /^\d+$/) { # Numeric answer? if ($answer==4) { print "\nYeah! You finally got it right. 2+2 = $answer.\n"; exit; } else { print "Good try, but wrong. 2+2 is not $answer. Try again.\n"; } } elsif ($answer=~/^a(bort)?$/i || $answer=~/^q(uit)?$/i) { print "Giving up, huh. Ok, bye.\n"; last; } elsif ($answer=~/^r(etry)?$/i) { print "I said, what's 2+2?\n"; } else { print "Invalid response ($answer).\n"; } } else { # print "Empty answer detected.\n"; } }