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

Hi!

Pls help! My code is not working! Pls tell me whats wrong with it... it returns value 1 in all cases!!

%dbase = { apple => "fruit", brinjal => "vegetable" , coffee => "beverage", }; my @info; $info[0]="lemon"; $info[1]="juice"; $result = validate($info[0],$info[1]); sub validate { my $usr = @_[0]; my $pwd = @_[1]; while (($u, $p) = each %dbase) { if ($usr == $u && $pwd == $p) { return(1); break; } else { return(0); } } } {print "$result"};

Thanks

BG

Replies are listed 'Best First'.
Re: sub always returns 1
by ikegami (Patriarch) on Mar 04, 2010 at 06:13 UTC
    First, always use
    use strict; use warnings;
    • You're comparing strings with the numerical comparison operator. Use eq instead of ==.

    • Well, you would be if you had actually populated your hash properly. You only assign one element to your hash, a reference to another anonymous hash. You need to assign a list of key-value pairs. Change = { } to = ( ).

    • Iterating through the entire hash defies the purpose of using a hash, especially when you know the key you want to locate.

    • Perl doesn't have a break keyword. C's break is called last in Perl.

    • Code that comes after a return doesn't get executed. That break is never reached.

    Fix:
    use strict; use warnings; my %dbase = ( apple => "fruit", brinjal => "vegetable", coffee => "beverage", ); sub validate { my ($usr, $pwd) = @_; return exists($dbase{$usr}) && $dbase{$usr} eq $pwd; } my @info = ( "lemon", "juice" ); my $result = validate(@info) ? "ok" : "forbidden"; print "$result\n";
      Thank you! Thank you! Thank you!
Re: sub always returns 1
by ungalnanban (Pilgrim) on Mar 04, 2010 at 06:54 UTC
    See the following Example
    #use strict; #use warnings; my $var = "Hello World"; if ($var == "Hello World"){ print "Equal\n"; } if ($var eq "Hello World"){ print "Strings are Equal\n";} if ($var == "Hello world"){ print "Equal\n"; }

    Here I didn't use the "use strict" and "use warnings".
    so the program give the output
    Output:
    Equal
    Strings are Equal
    Equals

    if I used the "use strict" and "use warning".
    Then the output of the program is (different).
    Output
    Equal
    Strings are Equal
    Argument "Hello world" isn't numeric in numeric eq (==) at equal.pl line 21.
    Equal


    --sugumar--
Re: sub always returns 1
by Anonymous Monk on Mar 04, 2010 at 06:13 UTC
    • Read perlintro, == is for comparing numbers, eq is for comparing strings
    • you're not supposed to store passwords unencrypted :)

      <snark>You're not supposed to store passwords encrypted either.</snark>

      Store a hash of the password and compare those.

      There is not a benefit to storing the password in any decryptable form, unless you need it to access something else (in which case, there are many times some other option). The user is, in effect, trusting the application and its storage and everything that the application depends on with whatever credentials it is providing.

      Under the principle of least needed privilege, you only store sufficient information to meet your needs - in most cases just to verify the user.

      Of course, this is now way off topic from the original post.

      It is said that "only perl can parse Perl." I don't even come close until my 3rd cup of coffee. --MidLifeXis