Robert's Perl Tutorial

http://www.sthomas.net/roberts-perl-tutorial.htm


Equality and Perl

Now pay close attention, otherwise you'll end up posting an annoying question somewhere. This is a FAQ, as in a Frequently Asked Question.

The symbol = is an assignment operator, not a comparison operator. Therefore:

So far we have been testing numbers, but there is more to life than numbers. There are strings too, and these need testing too.

$name	 = 'Mark';

$goodguy = 'Tony';

if ($name == $goodguy) {
        print "Hello, Sir.\n";
} else {
        print "Begone, evil peon!\n";
}
   

Something seems to have gone wrong here. Obviously Mark is different to Tony, so why does perl consider them equal?

Mark and Tony are equal -- numerically. We should be testing them as strings, not as numbers. To do this, simply substitute == for eq and everything will work as expected.