sub exec_command { my ($cmd_login, $ssh_bin, $startTime, $duration, $auth) = @_; system("doConnect() $cmd_login $ssh_bin $startTime $duration $auth"); } while (...) { my $cmd_login = "$user\@$host"; my ssh_bin = `which ssh`; chomp $ssh_bin; ... exec_command($cmd_login, $ssh_bin, $startTime, $duration, $auth); }