sub slurp { my $fname = shift; open my $FILE, '<', $fname; local $/; my $key = <$FILE>; close $FILE; return $key; }
You should verify that the file opened correctly and take appropriate action if it didn't rather than trying to use a possibly invalid filehandle.
You should chomp the data returned here rather than in another subroutine.
sub slurp { my $fname = shift; local $/; open my $FILE, '<', $fname or die "Cannot open '$fname' becaus +e: $!"; chomp( my $key = <$FILE> ); return $key; }
sub has_key { my ($key, $keys) = @_; chomp($key); my $suc = 0; my @keys = split /\n/,$keys; my $i = 1; foreach my $k (@keys) { chomp($k); if($key eq $k) { $suc = 1; last; } else { $suc = 0; } $i++; } return $suc; }
The $i variable is superfluous in this subroutine.
You are spliting, and thus removing newlines from @keys and then you are chomping the elements of @keys to remove newlines that aren't there anymore?
The else clause in the loop is totally superfluous because you exit the loop as soon as you change the value of $suc.
sub has_key { my ( $key, $keys ) = @_; foreach my $k ( split /\n/, $keys ) { return 1 if $key eq $k; } return 0; }
Or:
sub has_key { my ( $key, $keys ) = @_; return scalar grep $key eq $_, split /\n/, $keys; }
In reply to Re: SSH Keyer for passwordless logins.
by jwkrahn
in thread SSH Keyer for passwordless logins.
by Sir mXe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |