in reply to SSH Keyer for passwordless logins.

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; }

Replies are listed 'Best First'.
Re^2: SSH Keyer for passwordless logins.
by Sir mXe (Acolyte) on May 26, 2011 at 19:09 UTC
    To address a few of the things you said:

    Thanks for reminding me about putting some sort of error handling in. I had to grok this out of some code that it was integrated into, and as such, I forgot to fix a few things with it.


    I definitely can see a lot of superfluous elements in the code, but that's because those elements were all there originally for testing purposes and I forgot to yank them out. Previously, the else clause was there to output the current key, and compare it to the key that was provided. Nonetheless, I needed to fix up some code in it anyways, so, thanks for looking through and letting me know where I tripped up.


    Also, for some reason or another, when I hadn't chopped the data, even in something already split, it still wasn't matching. My theory is that there may have been an extra character in there that I may have bee nforgetting. Nonetheless. I appreciate your input, and I will put that in my code to get everything back to how it should be. :D