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


in reply to Subroutine Loop

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;} }
Also, believe me using strict and warnings will save you a world of hurt!

Replies are listed 'Best First'.
Re^2: Subroutine Loop
by tryingnottofailmytes (Novice) on Dec 06, 2015 at 18:32 UTC
    Thanks everyone! I didn't realize that reg ex wasn't reading the whitespace b/c said It hand to end with a new line. also rest assured that I AM USING -W AND USE STRICT; I just have a lot of programs in one file and didn't copy it in.