Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Subroutine Loop

by QuillMeantTen (Friar)
on Dec 06, 2015 at 16:59 UTC ( #1149520=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1149520]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-05-29 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?