I guess i might need to in case of typo?
Yes, exactly. But since you already have an array of valid PIDs, it might be a better idea to change the logic of the subroutine and present each PID to the user instead. Like this:
my @pids = ...;
...
kill_processes(\@pids);
...
sub kill_processes
{
my $pids = shift;
for my $pid (@$pids)
{
print "Kill process $pid? ([y]es, [n]o, [q]uit)?\n";
my $ans = <>;
last if $ans =~ /^Q/i;
next unless $ans =~ /^Y/i;
`kill $pid`;
print "Process $pid killed\n";
}
}
Using this approach there is no possibility of a user-input typo. And as a bonus, the loop logic is simpler.
Hope that helps,
|