in reply to Repeat question

IO::Prompt::Hooked based solution:

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; }

It's not a great solution, but it's pretty easy to work with.

It might be worth explaining: The call to prompt only happens one time per random number, and the validation hook will cause prompt to re-query automatically until the validation hook passes.


Dave