Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl compare strings with "=="

Tags:

perl

In perl, one should compare two strings with "eq" or "ne" etc.

I am a little surprised the following code snippet will print "yes".

$str = "aJohn";
$x = substr($str, 1);
if ($x == "John") {
    print "yes\n";
}

My perl has version v5.18.4 on Ubuntu.

Is there a case where the "==" on two strings produce a different result from "eq"? Thanks.

like image 562
packetie Avatar asked Nov 22 '16 01:11

packetie


1 Answers

"foo" == "bar" is true. "foo" eq "bar" is false.

The reason for this: == is numeric comparison. "foo" and "bar" are both numerically evaluate to 0 (like "17foo" evaluates numerically to 17); since 0 == 0, "foo" == "bar". This is not normally the operation you are looking for.

like image 141
Amadan Avatar answered Oct 03 '22 14:10

Amadan