Try taking a look at IO::Select, and modifying your design
to do non-blocking io with the various file handles.
Something like:
use strict;
use warnings;
use IO::Handle;
use IO::Select;
my $s = IO::Select->new();
...
$s->add(0); # STDIN
$s->add($telnet->fileno());
$s->add($telnet2->fileno());
while(1) {
my @ready = $s->can_read();
last unless @ready;
foreach my $fd (@ready) {
&handleTelnet1() if $telnet->fileno() == $fd;
&handleTelnet2() if $telnet2->fileno() == $fd;
&handleStdin() if 0 == $fd;
}
}
...
The above code does assume that your $telnet handles are IO::Handle
objects.
Updated:
robin
pointed out some bugs in the example code relating
to stdin. Thanks.