while (1) { my @random_int = sort {$b <=> $a} map {int(rand(100))} 0..1; my $correct = 0; last if !defined prompt( message => "What is $random_int[0] - $random_int[1]? ('q' to exit) ", validate => sub { my $raw = shift; return 0 unless looks_like_number($raw); return $correct = $raw == $random_int[0] - $random_int[1]; }, error => sub { my $raw = shift; return looks_like_number($raw) ? "Sorry, you are wrong. Have another go.\n" : "You need to enter a number.\n"; }, escape => qr/^q$/i, ); print "Well done! You are correct!\n\n" if $correct; }