http://qs321.pair.com?node_id=1215333


in reply to Re: Looking for a loop
in thread Looking for a loop

Translated to perl:

#!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; sub choose { say "choose a number:"; say for 1..5; } my ($correct, $response); while(!$correct){ choose(); chomp($response = <>); if($response >= 1 && $response <= 5){ $correct = 1; } else{ say "You have chosen the wrong number"; } }

But why not get rid of $correct, since you can just exit the loop?

#!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; sub choose { say "choose a number:"; say for 1..5; } my $response; while(1){ choose(); chomp($response = <>); last if $response >= 1 && $response <= 5; say "You have chosen the wrong number"; }