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

First, some background. This is part of a script I am writing to make a user in the system, and an SQL database. It makes users fine, but I haven't began on the SQL part yet.

This is part of a particular subroutine I wrote to get the password for the user from the admin- I know it is not perfect but this is a work in progress.

do
{
    print "Enter password: ";
    chomp ($pw1 = <STDIN>);
    print "Enter it again, I don't trust you: ";
    chomp ($pw2 = <STDIN>);
} while (($pw1 ne $pw2) && ($pw1 ne ""));

This piece of code works fine- my question is this: at first it did not function correctly, regardless of what was entered both times the first pw would be passed back. Changing to ne from != fixed it up. I thought they were the same thing, whats up?

-r0bb

Replies are listed 'Best First'.
Re: Require some subtle wisdom
by kilinrax (Deacon) on Jan 22, 2001 at 17:53 UTC
    'ne' compares them as strings, '!=' as numbers.
    Any non-numeric string evaluates to zero, numerically, hence '!=' would (nearly) always claim two strings were equal.
Re: Require some subtle wisdom
by clemburg (Curate) on Jan 22, 2001 at 17:56 UTC

    Perl has different operators for comparing strings and for comparing numbers. ne is for strings, and != is for numbers. See perldoc perlop on your favorite command line.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: Require some subtle wisdom
by tame1 (Pilgrim) on Jan 22, 2001 at 22:16 UTC
    != and = are designed around numbers. Your text was just that - text. ne, eq and such are designed around text. So, when comparing numbers, use math symbols. When using text, use words.

    What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"
Re: Require some subtle wisdom
by repson (Chaplain) on Jan 23, 2001 at 04:56 UTC
    I'm not sure your while condition does what you want.
    You probably meant
    while (($pw1 ne $pw2) || ($pw1 eq ""))
    I'd probably make that
    until ($pw1 && ($pw1 eq $pw2))
    Just a bit clearer, and correct.