jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

Hola monqueros,
When I run this code I get a single key and value for the %passfile (followed by the value of $check_me). This is in keeping with my suspicion that I am not getting everything into %passfile that I thought I was. Though when I printed the keys and values using while (my ($k, $v) = each %passfile) { I got all everthing I could see in the file.

I am using Data::Dumper for the first time and I am unsure whether my error is in the way I create my hash or in the way I try to Dump it. Here is the sub which creates the hash:
sub read_passfile { %passfile = (); open (PF, "$pass_file") or die "Couldn't read password file: $!"; while (<PF>) { chomp; @_ = split("\t", $_); $passfile{ $_[0] } = $_[1]; close PF; } }

And here is the Data::Dumper code.
my $check_me = my $check_me = $form_data{ 'new_user_name' }; my $h = \%passfile; my @c = [$h, $check_me]; my $d = Data::Dumper->new(@c, \@c); print $q->header; print $d->Dump; exit;

Any advice appreciated.
TIA
jg
_____________________________________________________
It's not my tree.

Replies are listed 'Best First'.
Re: Bad file slurp or Bad Dumper code?
by derby (Abbot) on Mar 15, 2002 at 14:16 UTC
    jerrygarciuh,

    You might want to move that close call outside of the loop:

    while (<PF>) { chomp; @_ = split("\t", $_); $passfile{ $_[0] } = $_[1]; } close PF;

    -derby

      Arrrrrrrrrrrrrrrrrrghh. It hurts to be this stupid!
      Thanks derby
      jg
      _____________________________________________________
      It's not my tree.
Re: Bad file slurp or Bad Dumper code?
by tachyon (Chancellor) on Mar 15, 2002 at 14:23 UTC

    ?Bad dumper code? but you need the close PF outside the while loop.

    use Data::Dumper; use CGI; $q = new CGI; %passfile = ( 'me' => 'secret7', 'you' => 'foo' ); print $q->header; print '<pre>' . $q->escapeHTML(Dumper(\%passfile)) . '</pre>';

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Bad file slurp or Bad Dumper code?
by erikharrison (Deacon) on Mar 15, 2002 at 16:39 UTC

    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

(jeffa) Re: Bad file slurp or Bad Dumper code?
by jeffa (Bishop) on Mar 15, 2002 at 18:34 UTC
    TIMTOWTDI
    use strict; my %password = %{ read_passfile('/etc/passwd') }; sub read_passfile { open (PF, shift) or die "Couldn't read password file: $!"; return { map {chomp;split(':',$_,2)} <PF> }; }
    I am returning a reference to the hash instead. Replace ':' with your delimeter of choice, of course.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)