use IO::Socket; sub connect_server { my $sock = IO::Socket::INET->new( PeerAddr => "localhost:8888", ) or die "$0: can't connect: $!\n"; return $sock; } my @inputs = qw( foo fooed fooen ); for my $input (@inputs) { my $conn = connect_server(); print $conn "$input\n"; # send the query my $reply = <$conn>; # read the response close $conn; print "found '$input' in lexicon\n" if $reply eq "FOUND\n"; }