Greetings,
Problem seems to be with your regex in the loop:
you only continue if the answer matches 0 or more whitespaces!
also since you chomp your input it wont match the newline character.
here is a fix
#!/usr/bin/perl use autodie; use strict; use warnings; my $messagetouser= "What is your favorite Color?"; my $ans = tryagain5 ( $messagetouser ); if ($ans) { print "$ans\n"; } else { print "You didn't reply! Goodbye.\n"; exit; } #print $answer, "\n"; sub callandresponse{ my ($messagetouser)=@_; print $messagetouser,"\n"; while(<STDIN>){ chomp $_; return $_; } } sub tryagain5 { my($messagetouser)=@_; my $ans = callandresponse ($messagetouser); my $count=1; while ($count < 5 && !($ans=~ /\A\s*\z/)) { $count++; $ans = callandresponse ($messagetouser); } if ($ans =~ /^\s*\n/) { return 0; } else { return $ans;} }
In reply to Re: Subroutine Loop
by QuillMeantTen
in thread Subroutine Loop
by tryingnottofailmytes
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |