Yes, you do need to move the close out of the while loop. However I have a couple of questions:

1) Why are you assigning to @_ ? @_ is a special Perl var, and it is confusing (to me at any rate) to see you using it as a simple scratch array.

2)Why aren't you returning the hash? If a subroutine has no return value, I find that the main code is confusing because there is the magic hash (or other var) that appears and I don't know to associate it with the routine that created it.

Additionally the use of globals (%passfile and $pass_file) is generally bad practice, and should be avoided.

Here's how I would write it (just a suggestion mind you)

sub read_passfile { my $file_name = shift; #Pass the file name to the routine my @temp; my %passfile = (); open (PF, $file_name) or die "Couldn't read password file: $!"; while (<PF>) { chomp; @temp = split("\t", $_); $passfile{ $temp[0] } = $temp[1]; } close PF; return %passfile; }
Cheers,
Erik

Update:Fixed a silly typo. Thanks jeffa


In reply to Re: Bad file slurp or Bad Dumper code? by erikharrison
in thread Bad file slurp or Bad Dumper code? by jerrygarciuh

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.