954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

wy wont my while loop work?

use strict;
use warnings;
use IO::Socket;

my $sock = new IO::Socket::INET(
									PeerAddr=> 'localhost',
									PeerPort=> '7070',
									Proto => 'tcp');

die "Could not create socket: $!\n" unless $sock;

my $run = "yes";
while($run eq "yes")
{

my $text = <STDIN>;
print $sock "$text";
print "Send more strings? yes/no";
$run = <STDIN>;
}
close($sock);


i have an while loop so that the script keeps running and you can enter text again for sending but i wont work why?

xellos
Newbie Poster
11 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Your loop runs while($run eq "yes") but when the user enters yes at the prompt then $run contains yes with a newline character at the end. Try doing a chomp on the $run after assigning the STDIN to it.

print "Send more strings? yes/no";
$run = <STDIN>;
chomp($run);#Remove the newline character from input
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You