my %connecting = map { $_ => 1 } @hosts; while (1) { for my $host (keys %connecting) { my $ssh = $ssh{$host}; if ($ssh->wait_for_master(1)) { delete $connecting{$host}; } elsif ($ssh->error) { # handle connection error delete $connecting{$host}; } } last unless %connecting; select (undef, undef, undef, 0.05); }