in reply to sub always returns 1
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.
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sub always returns 1
by blackgoat (Acolyte) on Mar 04, 2010 at 06:40 UTC |