in reply to Problem comparing SHA1 string from db, with hashed login password

my $saved_pass = 'bd0e9f94ce671b3cdd13081fa5a8b32f9ccd9ebf'; my $eword = 'bd0e9f94ce671b3cdd13081fa5a8b32f9ccd9ebf'; my $passwd_check; $saved_pass eq $eword ? $passwd_check = "good" : $passwd_check = "fail"; print $passwd_check;
fail
my $saved_pass = 'bd0e9f94ce671b3cdd13081fa5a8b32f9ccd9ebf'; my $eword = 'bd0e9f94ce671b3cdd13081fa5a8b32f9ccd9ebf'; my $passwd_check = $saved_pass eq $eword ? "good" : "fail"; print $passwd_check;
good

I have do admit that I don't really get why. Anyone care to explain?
update: Got it (from perlop):
Because this operator produces an assignable result, using assignments without parentheses will get you in trouble. For example, this:
$a % 2 ? $a += 10 : $a += 2
Really means this:
(($a % 2) ? ($a += 10) : $a) += 2
Rather than this:
($a % 2) ? ($a += 10) : ($a += 2)
so what you wrote really means:
($saved_pass eq $eword ? $passwd_check = "good" : $passwd_check ) += "fail";
perlop also says that you can assign to the ternary operator if both arguments between the ? and : are valid lvalues.
So the code comes down to:
if ($saved_pass eq $eword) { $passwd_check = "fail"; }else{ $passwd_check = "fail"; }
In words: assign "fail" to $passwd_check or $passwd_check :-)

Replies are listed 'Best First'.
Re^2: Problem comparing SHA1 string from db, with hashed login password
by hsinclai (Deacon) on Apr 16, 2005 at 03:24 UTC
    Golo, thanks for catching and explaining this - now I understand - it's amazing how long I've misunderstood this operator.

    I've never read past the
    If the argument before the ? is true, the argument before the : is ret +urned, otherwise the argument after the : is returned.
    part of the docs.

    Your example
    my $passwd_check = $saved_pass eq $eword ? "good" : "fail";
    makes my login page work perfectly now.. thanks again.

    Harold