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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.